Python

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

Share this for your friends.

The reason of 'ValueError: The truth value of ... 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 ... is ambiguous' in NumPy or Pandas".

Author


Mid-carieer engineer (AI, system). Good at Python and SQL.

Advantage to read

You can understand about the reason of 'ValueError: The truth value of ... is ambiguous' in NumPy or Pandas. Then you don't have to concern about programming with pandas.DataFrame or NumPy.ndarray.


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

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

It is because you used and, or, not 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.

DATA

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 and, or, not.

You can see the error like below.

SAMPLE

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 operetion ?



Solution for "ValueError: The truth value of ... is ambiguous"

Object-wise operator and, or, not cause the error.

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


The solution is that using &, |, ~.

For example, use & instead of and.

Then you can get the result below.

SAMPLE

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.

SAMPLE

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.

SAMPLE

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 operetion.



Conclusion

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

Points are below.

Point

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



Remember it. So it'll be easy to use.


There are some other articles about pandas.Dataframe or NumPy.ndarray.

If you interested in them, please read them.


Share this for your friends.

If you felt this article is useful, please share.

にほんブログ村 IT技術ブログへ

-Python
-,

© 2023 ITips