Python

NumPyやPandasで「ValueError: The truth value of ... is ambiguous」になる原因

Share this for your friends.

NumPyやPandasで「ValueError: The truth value of ... is ambiguous」になる原因

pandasで論理演算しようとしたらなんか ValueError とかいうのが出たんだけど…


NumPyやPandasで要素の論理演算をしようとしてこんなエラーが出たことはないだろうか。

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


何故このようなエラーが起こるのか。

どのように対処すればよいのか。

そこで今回はNumPyやPandasで「ValueError: The truth value of ... is ambiguous」になる原因について紹介する。

この記事を書いている人


システムエンジニア、AIエンジニアと、IT業界で10年以上働いている中堅。PythonとSQLが得意。

記事を読むメリット

NumPyやPandasで「ValueError: The truth value of ... is ambiguous」になる原因がわかり、PandasやNumPyでのエラー対応が得意になる


NumPyやPandasで「ValueError: The truth value of ... is ambiguous」になる原因

NumPyやPandasで「ValueError: The truth value of ... is ambiguous」になる原因、それはNumPyやPandasは複数の要素を持つのに and, or, not 等でオブジェクト単位に論理判定しようとしているから


ではサンプルを見てみる。

まずはbool値を含むデータを用意する。

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


そしてbool値を含むカラムに対して and, or, not で論理演算してみるとエラーになる。

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().


どれも ValueError: The truth value of ... is ambiguous のエラーを発生させる。

要素同士を論理演算したい場合にはどうしたらよいのだろうか。



「ValueError: The truth value of ... is ambiguous」の対処法

NumPyやPandasでカラムに対して and, or, not ですると、オブジェクト単位では論理演算できないのでエラーになる。

では要素単位で論理演算したい場合はどうすればよいのか。


方法は &, |, ~ を使うこと。

例えば and の代わりに & を使うと以下のようになる。

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


また 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


また 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


and, or, not の代わりに &, |, ~ を使うことで要素単位の論理演算ができる。



まとめ

今回はNumPyやPandasで「ValueError: The truth value of ... is ambiguous」になる原因について紹介した。

ポイントは以下の通り。

ココがポイント

  • and, or, not はオブジェクト単位の演算なので複数要素を持つNumPyやPandasの論理演算ができずエラーになる
  • and, or, not の代わりに &, |, ~ を使うことで要素単位の論理演算ができる



覚えてしまえば簡単なんですけどね


他にもpandas関連の記事もあるので、もし気になるものがあれば見てみて欲しい


Share this for your friends.

ITipsと同じようなブログを作る方法

ブログに興味がありますか?

もしブログに興味がある場合は↓このページ↓を参考にすれば、ITipsと同じ構成でブログを作ることができます

サーバー、ドメイン、ASPと【ブログに必要なものは全て】このページに書きました。
同じ構成でブログ作るのはいいけど、記事はマネしないでネ (TДT;)

ランキング参加中

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

他にもブログやSNSで紹介してくれると励みになります。

はてブのコメントで酷評されると泣きます(´;ω;`)

-Python
-,

© 2024 ITips