博客
关于我
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/

你可能感兴趣的文章
netty底层源码探究:启动流程;EventLoop中的selector、线程、任务队列;监听处理accept、read事件流程;
查看>>
Netty核心模块组件
查看>>
Netty框架的服务端开发中创建EventLoopGroup对象时线程数量源码解析
查看>>
Netty源码—2.Reactor线程模型一
查看>>
Netty源码—4.客户端接入流程一
查看>>
Netty源码—4.客户端接入流程二
查看>>
Netty源码—5.Pipeline和Handler一
查看>>
Netty源码—6.ByteBuf原理二
查看>>
Netty源码—7.ByteBuf原理三
查看>>
Netty源码—7.ByteBuf原理四
查看>>
Netty源码—8.编解码原理二
查看>>
Netty源码解读
查看>>
Netty的Socket编程详解-搭建服务端与客户端并进行数据传输
查看>>
Netty相关
查看>>
Network Dissection:Quantifying Interpretability of Deep Visual Representations(深层视觉表征的量化解释)
查看>>
Network Sniffer and Connection Analyzer
查看>>
NetworkX系列教程(11)-graph和其他数据格式转换
查看>>
Networkx读取军械调查-ITN综合传输网络?/读取GML文件
查看>>
Net与Flex入门
查看>>
net包之IPConn
查看>>