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

你可能感兴趣的文章
nginx 常用配置记录
查看>>
Nginx 我们必须知道的那些事
查看>>
Nginx 的 proxy_pass 使用简介
查看>>
Nginx 的配置文件中的 keepalive 介绍
查看>>
nginx 配置 单页面应用的解决方案
查看>>
nginx 配置~~~本身就是一个静态资源的服务器
查看>>
Nginx下配置codeigniter框架方法
查看>>
nginx添加模块与https支持
查看>>
Nginx的Rewrite正则表达式,匹配非某单词
查看>>
Nginx的使用总结(一)
查看>>
Nginx的是什么?干什么用的?
查看>>
Nginx访问控制_登陆权限的控制(http_auth_basic_module)
查看>>
nginx负载均衡的五种算法
查看>>
Nginx配置ssl实现https
查看>>
Nginx配置TCP代理指南
查看>>
Nio ByteBuffer组件读写指针切换原理与常用方法
查看>>
NI笔试——大数加法
查看>>
NLP 基于kashgari和BERT实现中文命名实体识别(NER)
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
Node.js安装与配置指南:轻松启航您的JavaScript服务器之旅
查看>>