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

你可能感兴趣的文章
oracle 可传输的表空间:rman
查看>>
Oracle 启动监听命令
查看>>
Oracle 启动阶段 OPEN
查看>>
Oracle 在Drop表时的Cascade Constraints
查看>>
Oracle 在Sqlplus 执行sql脚本文件。
查看>>
Oracle 如何处理CLOB字段
查看>>
oracle 学习
查看>>
oracle 定义双重循环例子
查看>>
ORACLE 客户端工具连接oracle 12504
查看>>
Oracle 客户端连接时报ORA-01019错误总结
查看>>
oracle 导出sql数据库表结构,使用sql developer 导出Oracle数据库中的表结构
查看>>
oracle 嵌套表 例子,Oracle之嵌套表(了解)
查看>>
Oracle 常用命令
查看>>
Oracle 常用的V$视图脚本(二)
查看>>
Oracle 并行原理与示例总结
查看>>
oracle 并集 时间_Oracle集合运算符 交集 并集 差集
查看>>
Oracle 序列sequence 开始于某个值(10)执行完nextval 发现查出的值比10还小的解释
查看>>
ORACLE 异常错误处理
查看>>
oracle 执行一条查询语句,把数据加载到页面或者前台发生的事情
查看>>
oracle 批量生成建同义词语句和付权语句
查看>>