Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

186
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda