ValueError: The truth value of a DataFrame is ambiguous in Python
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?
-
N0
Nguyen Quoc Viet May 30 2021
The
or
andand
python statements requiretruth-values
. Forpandas
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|
-
A0
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.
* Type maximum 2000 characters.
* All comments have to wait approved before display.
* Please polite comment and respect questions and answers of others.