Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

why 'if' statement does not pick the specific row for condition from a data frame in python

writing a function that should meet a condition on a row basis and return the expected results

def bt_quantity(df):
    df = bt_level(df)
    df['Marker_change'] = df['Marker'] - df['Marker'].shift(1).fillna(0).round(0).astype(int)
    df['Action'] = np.where(df['Marker_change'] > 0, "BUY", "")

    def turtle_split(row):
        if df['Action'] == 'Buy':
            return baseQ * (turtle ** row['Marker'] - 1) // (turtle - 1)
        else:
            return 0
    df['Traded_q'] = df.apply(turtle_split, axis=1).round(0).astype(int)
    df['Net_q'] = df['Traded_q'].cumsum().round(0).astype(int)
    print(df.head(39))
    return df

This is a common issue, and I am not using any "and" or "or" in the code.
still getting the below error

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

I tried changing str to int(BUY >> 1), no progress.
P.S. the data set is huge and I am using multiple modules and functions to work on this project.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

As you said, this is really a common problem, you will find the answers from
Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()

But to address your particular case, this is not a very efficient way of doing it as the dataset is huge. You should use Numpy. That will shorten the runtime drastically.

I see two issues with your snippet

  1. There is a Typo. You used "BUY" and then used "Buy". Python is case-sensitive.
  2. The col Action is getting tested for "BUY" entirely. The fix is to use row (Not a pythonic way, but a small fix)
def bt_quantity(df):
    df = bt_level(df)
    df['Marker_change'] = df['Marker'] - df['Marker'].shift(1).fillna(0).round(0).astype(int)
    df['Action'] = np.where(df['Marker_change'] > 0, "BUY", "")

    def turtle_split(row):
        if row['Action'] == 'BUY':
            return baseQ * (turtle ** row['Marker'] - 1) // (turtle - 1)
        else:
            return 0
    df['Traded_q'] = df.apply(turtle_split, axis=1).round(0).astype(int)
    df['Net_q'] = df['Traded_q'].cumsum().round(0).astype(int)
    print(df.head(39))
    return df
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading