pandas进行多条件过滤时可能出现的优先级bug,导致程序无法运行
2023-09-11 14:20:29 时间
pandas进行多条件过滤时可能出现的优先级bug,导致程序无法运行
当我们进行pandas 多条件过滤时,可能会出像这样的报错
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() 和 cannot compare a dtyped [float64] array with a scalar of type [bool]
诸如下面的代码会出错
实例代码
print(df[df['Survived']==1 & df['Age']>30])
写法不一样会报不同的错误
print(df[ df['Age']>30 & df['Survived']==1])
这两种写法分别对应上面两个报错
cannot compare a dtyped [float64] array with a scalar of type [bool] 和 ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any()
原因就在于运算符 == 和 < 优先级 顺序小于 & 所以犯了很严重的错误
那么如何解决呢
改成如下代码,通过()更改优先级顺序就可以达到多条件过滤的目的
print(df[(df['Age']>30) & (df['Survived']==1)])
相关文章
- ProGuard的作用、使用及bug分析
- 360浏览器各历史版本大全和bug汇总(及历史版本官方下载地址)
- 脏内存导致BUG的问题
- 【MySQL】全索引扫描的bug
- bug-sqlite3
- bug_ _ 常见的bug1
- EasyPlayer RTSP Android安卓播放器修复播放画面卡在第一帧bug
- DayDayUp:平均每篇文章1毛! 本博主自2020年6月1日起,如有任何问题可在博客贴吧留言或者私信博主(包括并不限于GUI软件编写、安装及编程语言中的bug、AI算法设计等),非诚勿扰!
- Python之Pandas:pandas.DataFrame()函数的简介、具体案例、使用方法详细攻略
- 如何从头到脚彻底解决一个MySQL Bug
- 修正EasyUI的BUG——Form中存在FileBox时的数据载入错误
- 如何提交一个高质量的bug? | 纯干货
- 软件测试遇到bug怎么分析,看看字节5年老司机是怎么秀操作的...