博客
关于我
pandas最后出现的gotchas问题的原因及解决方法
阅读量:530 次
发布时间:2019-03-08

本文共 1107 字,大约阅读时间需要 3 分钟。

  

官方文档中提到的一个pandas错误示例

        >>> if pd.Series([False, True, False]):        ...     print("I was true")    

产生了一个不 обычно的错误信息

        Traceback ...        ValueError: The truth value of an array is ambiguous. Use a.empty, a.any() or a.all().    

错误原因分析

if后跟的条件判断涉及到了pandas Series对象的布尔判断。由于Series对象的结果具有数组结构,含有True和False值

这种情况表明,无论判断结果是True还是False,都存在数据中的真值或者假值,从而导致逻辑判断的不确定性

解决方法

        if pd.Series([False, True, False]) is not None:            print('i am not none')    

或使用等价的方法进行布尔判断

        if pd.Series([False, True, False]).any():            print('any of the series is true')        if pd.Series([True, False, True]).all():            print('all values are true')    

对于单元素对象的布尔判断,可以直接使用bool()方法

        In [11]: pd.Series([True]).bool()        Out[11]: True        In [12]: pd.Series([False]).bool()        Out[12]: False                In [13]: pd.DataFrame([[True]]).bool()        Out[13]: True        In [14]: pd.DataFrame([[False]]).bool()        Out[14]: False    

转载地址:http://psziz.baihongyu.com/

你可能感兴趣的文章
openstack虚拟机迁移live-migration中libvirt配置
查看>>
OpenStack项目管理实战
查看>>
OpenStreetMap初探(一)——了解OpenStreetMap
查看>>
openSUSE 13.1 Milestone 2 发布
查看>>
openSUSE推出独立 GUI 包管理工具:YQPkg,简化了整个软件包管理流程
查看>>
OpenVP共用账号 一个账号多台电脑登录
查看>>
OpenVSwtich(OVS)Vlan间路由实战 附实验环境
查看>>
Openwrt LuCI模块练习详细步骤
查看>>
openwrt_git_pull命令提示merger冲突时如何解决?
查看>>
OpenWrt包管理软件opkg的使用(极路由)
查看>>
OpenWrt固件编译刷机完全总结
查看>>
Open××× for Linux搭建之二
查看>>
Open×××有线网络时使用正常,无线网络时使用报错的解决方案
查看>>
Opera Mobile Classic Emulator
查看>>
Operation not supported on read-only collection 的解决方法 - [Windows Phone开发技巧系列1]
查看>>
OperationResult
查看>>
Operations Manager 2007 R2系列之仪表板(多)视图
查看>>
operator new and delete
查看>>
operator new 与 operator delete
查看>>
operator() error
查看>>