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)< code></=150)]>
解决:
将and改为&
df_filtered = df[(df['Shares']>=100) & (df['Shares']<=150)] df_filtered # print(df_filtered)< code></=150)]>
完整错误:
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().
Original: https://blog.csdn.net/zhongkeyuanchongqing/article/details/120870572
Author: Data+Science+Insight
Title: ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/638285/
转载文章受原作者版权保护。转载请注明原作者出处!