什么?pandas读取出来的数据不能用and进行筛选?

使用pandas读取数据后,想添加约束进行筛选
total_fr_month[total_fr_month[“name”] == i and total_fr_month[“year”] == 2014][“p_jcl”]

出现如下报错:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

查阅资料后得知pandas认为or、and不明确需要使用|、&按位操作

修改为
total_fr_month[total_fr_month[“name”] == i & total_fr_month[“year”] == 2014][“p_jcl”]

然后再次报错,哈哈,惊不惊喜,意不意外
TypeError: cannot compare a dtyped [int64] array with a scalar of type [bool]

&两边应该是bool型,然后又修改为&&
毫无疑问,再次报错invalid syntax,python里好像就没有这个东西
然后一想,两边就是bool类型啊!可能是,符号运行顺序问题,加了两个括号
最终版本
total_fr_month[(total_fr_month[“name”] == i) & (total_fr_month[“year”] == 2014)][“p_jcl”]

发布了16 篇原创文章 · 获赞 27 · 访问量 2719

猜你喜欢

转载自blog.csdn.net/weixin_42224119/article/details/95753387