用 pandas 的if语句抛出"ValueError:系列的真值是不明确的". [英] If-statement with pandas throws "ValueError: The truth value of a Series is ambiguous"
本文介绍了用 pandas 的if语句抛出"ValueError:系列的真值是不明确的".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想查看我的数据框中是否有针对某一列的特定字符串,如果有,请启动API.到目前为止的代码:
I am wanting to see if a particular string is present in my dataframe for one column and fire off an API if it is. code so far:
if new_df.col1.str.contains('string') == True:
POST REQUEST
elif new_df.col2.str.contains('string2') == True:
POST REQUEST
else:
print('not good')
我不断收到错误消息:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
推荐答案
发生此错误的原因是因为python中的if-else
表达式用于比较标量布尔值.您通过了系列赛.
The reason for this error is because if-else
expressions in python are meant to compare scalar boolean values. You passed a Series.
查看全文