Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

182
Views
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.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!