我想看看我的数据帧中的特定列中是否存在特定字符串.
我收到了错误
ValueError: The truth value of a Series is ambiguous. Use a.empty,
a.bool(), a.item(), a.any() or a.all().
import pandas as pd
BabyDataSet = [('Bob', 968), ('Jessica', 155), ('Mary', 77), ('John', 578), ('Mel', 973)]
a = pd.DataFrame(data=BabyDataSet, columns=['Names', 'Births'])
if a['Names'].str.contains('Mel'):
print "Mel is there"
解决方法:
a [‘Names’].str.contains(‘Mel’)将返回一个大小为len(BabyDataSet)的布尔值的指示符向量
因此,你可以使用
mel_count=a['Names'].str.contains('Mel').sum()
if mel_count>0:
print ("There are {m} Mels".format(m=mel_count))
或者任何(),如果您不关心有多少记录与您的查询匹配
if a['Names'].str.contains('Mel').any():
print ("Mel is there")
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。