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

Posted Data+Science+Insight

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.相关的知识,希望对你有一定的参考价值。

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 a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

问题:

解决:

完整错误:


问题:

出现此错误是因为Python的逻辑运算符(and、or、not)是用来与布尔值(boolean)一起使用的,所以当试图将它们与序列或数组一起使用时,系统程序不清楚如何确定它是真的还是假的,因此会导致ValueError。

import pandas as pd

data = 
    'Name': ['Microsoft Corporation', 'Google, LLC', 'Tesla, Inc.',\\
             'Apple Inc.', 'Netflix, Inc.'],
    'Symbol': ['MSFT', 'GOOG', 'TSLA', 'AAPL', 'NFLX'],
    'Industry': ['Tech', 'Tech', 'Automotive', 'Tech', 'Entertainment'],
    'Shares': [100, 50, 150, 200, 80]


df = pd.DataFrame(data)
# print(df)
df


df_filtered = df[(df['Shares']>=100) and (df['Shares']<=150)]
df_filtered
# print(df_filtered)

解决:

将and改为&

df_filtered = df[(df['Shares']>=100) & (df['Shares']<=150)]
df_filtered
# print(df_filtered)

 

完整错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-11-1f338a747fac> in <module>
----> 1 df_filtered = df[(df['Shares']>=100) and (df['Shares']<=150)]
      2 df_filtered
      3 # print(df_filtered)

D:\\anaconda\\lib\\site-packages\\pandas\\core\\generic.py in __nonzero__(self)
   1441     def __nonzero__(self):
   1442         raise ValueError(
-> 1443             f"The truth value of a type(self).__name__ is ambiguous. "
   1444             "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
   1445         )

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

参考:pandas

参考: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 a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.的主要内容,如果未能解决你的问题,请参考以下文章

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

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

python pandas进行条件筛选时出现ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.al

[React] Use the URL as the source of truth in React

The Truth About Threads(关于线程的真相 )