ValueError: The truth value of a DataFrame is ambiguous in Python

Dung Do Tien May 30 2021 326

I have an issue when I trying to filter the result dataframe with an OR operator.
I want to extract all column rsl_name values that satisfy the condition above 10 and below -10.

result = result[(result['rsl_name'] > 10) or (result['rsl_name'] < -10)]

The logic above give me an error is ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()

Please tell me why and how to fix it?

Have 2 answer(s) found.
  • N

    Nguyen Quoc Viet May 30 2021

    The or and and python statements require truth-values. For pandas these are considered ambiguous so you should use "bitwise" | (or) or & (and) operations:

    result = result[(result['var']>0.25) | (result['var']<-0.25)]

    In general. You need to change or to |

  • A

    Aa May 30 2021

    Well pandas use bitwise & | and each condition should be wrapped in a ()

    For example following works:

    data_query = data[(data['year'] >= 200) | (data['year'] <= 250)]

    I hope it resolves the issue for you.

Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close