Thanks to theidioms.com

[Solved] Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()

[Solved] Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()
Data Science

[Solved] Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()

The Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all() error comes up when you are trying to chain conditional statements on a Pandas DataFrame using and/or operators.

[Solved] Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()

Here are two cases when the error can come up:

# First Case
df[(df['col1'] > 20) and (df['col2'] <40)]

# Second Case
df[(df['col1'] > 20) or (df['col2'] <40)]

In both of the above two cases, the or and and Python conditional keywords require truth-values. However, in Pandas, these are considered ambiguous.

You can solve it by using bitwise operators such as | (or) or & (and) operators as follows:

# First Case
df[(df['col1'] > 20) & (df['col2'] <40)]

# Second Case
df[(df['col1'] > 20) | (df['col2'] <40)]

This will solve your Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all() error.


If you are interested in more things data, The Click Reader provides you a large collection of data science resources to study from—check it out by clicking here.

Leave your thought here

Your email address will not be published. Required fields are marked *