The reason of "ValueError: The truth value of a Series is ambiguous" in NumPy or Pandas

The reason of "ValueError: The truth value of a Series is ambiguous" in NumPy or Pandas

I tried to do logical operation with pandas.
Then it showed ValueError .

You might see such an error when you tried to do logical operation.

ValueError: The truth value of a Series is ambiguous.
Use a.empty, a.bool(), a.item(), a.any() or a.all().

Why does such kind of error happen ?

How can we solve it ?

So today I will introduce about "The reason of "ValueError: The truth value of a Series is ambiguous" in NumPy or Pandas".

目次

The reason of "ValueError: The truth value of a Series is ambiguous" in NumPy or Pandas

Why does the error "ValueError: The truth value of a Series is ambiguous" happen in NumPy or Pandas ?

It is because you used andornot with NumPy or Pandas.
They are not for element-wise logical operation, but for object-wise logical operation.

Let's see sample code.

First, prepare data that contains boolean column.

import pandas as pd

data_list1 = [
    ["a",True,True],
    ["b",True,False],
    ["c",False,True],
    ["d",False,False]
]
col_list1 = ["col1","col2","col3"]
df1 = pd.DataFrame(data=data_list1, columns=col_list1)
print(df1)

#   col1   col2   col3
# 0    a   True   True
# 1    b   True  False
# 2    c  False   True
# 3    d  False  False

Then do logical operation by andornot.

You can see the error like below.

df1["not_col2"] = not df1["col2"]
print(df1)
# ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

df1["and_col2_3"] = (df1["col2"] and df1["col3"])
print(df1)
# ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

df1["or_col2_3"] = (df1["col2"] or df1["col3"])
print(df1)
# ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

All of them occur ValueError: The truth value of ... is ambiguous.

How can we do element-wise logical operation ?

How to solve the error

Object-wise operator andornot cause the error.

So what should we do for element-wise logical operation ?

The solution is that using &|~.

For example, use & instead of and.

Then you can get the result below.

df1["and_col2_3"] = (df1["col2"] & df1["col3"])
print(df1)

#   col1   col2   col3  and_col2_3
# 0    a   True   True        True
# 1    b   True  False       False
# 2    c  False   True       False
# 3    d  False  False       False

Same as &, you can use | instead of or.

df1["or_col2_3"] = (df1["col2"] | df1["col3"])
print(df1)

#   col1   col2   col3  or_col2_3
# 0    a   True   True       True
# 1    b   True  False       True
# 2    c  False   True       True
# 3    d  False  False      False

Also you can use ~ instead of not.

df1["not_col2"] = ~df1["col2"]
print(df1)

#   col1   col2  not_col2
# 0    a   True     False
# 1    b   True     False
# 2    c  False      True
# 3    d   True     False

With using &|~, we can do element-wise logical operation.

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

python – Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all() – Stack Overflow

Conclusion

Today I will explained about "The reason of "ValueError: The truth value of a Series is ambiguous" in NumPy or Pandas".

Important points are below.

  • andornot are for object-wise logical operation. So when you use them with Pandas or Numpy, they cause error.
  • Instead of andornot, you can use &|~ for element-wise logical operation.

python – Logical operators for Boolean indexing in Pandas – Stack Overflow

Please remember this in case of facing the error.

The reason of "ValueError: The truth value of a Series is ambiguous" in NumPy or Pandas

この記事が気に入ったら
いいね または フォローしてね!

If you like this article, please share !
  • URLをコピーしました!
  • URLをコピーしました!

Author

karasanのアバター karasan System engineer

Mid-career engineer (AI, Data science, Salesforce, etc.).
Good at Python and SQL.

目次