警告和疑难意味着一个看不见的问题。在使用Pandas过程中,需要特别注意的地方。

与Pandas一起使用If/Truth语句

当尝试将某些东西转换成布尔值时,Pandas遵循了一个错误的惯例。 这种情况发生在使用布尔运算的。 目前还不清楚结果是什么。 如果它是真的,因为它不是zerolength? 错误,因为有错误的值? 目前还不清楚,Pandas提出了一个ValueError -

import pandas as pd

if pd.Series([False, True, False]):
print ('I am True')

输出结果:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
 

if条件,它不清楚如何处理它。错误提示是否使用None或任何这些。

import pandas as pd

if pd.Series([False, True, False]).any():
print("I am any")

要在布尔上下文中评估单元素Pandas对象,请使用方法.bool() -

import pandas as pd

print (pd.Series([True]).bool())

输出结果:

True
 

按位布尔值

按位布尔运算符(如==!=)将返回一个布尔系列,这几乎总是需要的。

import pandas as pd

s = pd.Series(range(5))
print (s==4)

输出结果:

0    False
1 False
2 False
3 False
4 True
dtype: bool
 

isin操作符

这将返回一个布尔序列,显示系列中的每个元素是否完全包含在传递的值序列中。

import pandas as pd

s = pd.Series(list('abc'))
s = s.isin(['a', 'c', 'e'])
print (s)

输出结果:

0     True
1 False
2 True
dtype: bool
 

重构索引与ix陷阱

许多用户会发现自己使用ix索引功能作为从Pandas对象中选择数据的简洁方法 -

import pandas as pd
import numpy as np df = pd.DataFrame(np.random.randn(6, 4), columns=['one', 'two', 'three','four'],index=list('abcdef')) print (df)
print ("=============================================")
print (df.ix[['b', 'c', 'e']])

输出结果:

        one       two     three      four
a -1.174632 0.951047 -0.177007 1.036567
b -0.806324 -0.562209 1.081449 -1.047623
c 0.107607 0.778843 -0.063531 -1.073552
d -0.277602 -0.962720 1.381249 0.868656
e 0.576266 0.986949 0.433569 0.539558
f -0.708917 -0.583124 -0.686753 -2.338110
=============================================
one two three four
b -0.806324 -0.562209 1.081449 -1.047623
c 0.107607 0.778843 -0.063531 -1.073552
e 0.576266 0.986949 0.433569 0.539558
 

这当然在这种情况下完全等同于使用reindex方法 -

import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(6, 4), columns=['one', 'two', 'three','four'],index=list('abcdef')) print (df)
print("=============================================")
print (df.reindex(['b', 'c', 'e']))

输出结果:

        one       two     three      four
a -1.754084 -1.423820 -0.152234 -1.475104
b 1.508714 -0.216916 -0.184434 -2.117229
c -0.409298 -0.224142 0.308175 -0.681308
d 0.938517 -1.626353 -0.180770 -0.470252
e 0.718043 -0.730215 -0.716810 0.546039
f 2.313001 0.371286 0.359952 2.126530
=============================================
one two three four
b 1.508714 -0.216916 -0.184434 -2.117229
c -0.409298 -0.224142 0.308175 -0.681308
e 0.718043 -0.730215 -0.716810 0.546039
 

有人可能会得出这样的结论,ixreindex是基于这个100%的等价物。 除了整数索引的情况,它是true。例如,上述操作可选地表示为 -

import pandas as pd
import numpy as np df = pd.DataFrame(np.random.randn(6, 4), columns=['one', 'two', 'three', 'four'],index=list('abcdef')) print (df)
print("=====================================")
print (df.ix[[1, 2, 4]])
print("=====================================")
print (df.reindex([1, 2, 4]))

输出结果:

        one       two     three      four
a 1.017408 0.594357 -0.760587 1.001547
b -1.480067 1.524270 0.455070 1.886959
c -0.136238 -0.165867 -0.589767 -1.078473
d 0.670576 1.600312 0.219578 -1.121352
e -0.224181 0.958156 0.013055 -0.013652
f 1.576155 -0.185003 -0.527204 -0.336275
=====================================
one two three four
b -1.480067 1.524270 0.455070 1.886959
c -0.136238 -0.165867 -0.589767 -1.078473
e -0.224181 0.958156 0.013055 -0.013652
=====================================
one two three four
1 NaN NaN NaN NaN
2 NaN NaN NaN NaN
4 NaN NaN NaN NaN
 

重要的是要记住,reindex只是严格的标签索引。这可能会导致一些潜在的令人惊讶的结果,例如索引包含整数和字符串的病态情况。

Pandas | 27 注意事项&窍门的更多相关文章

  1. numpy pandas 索引注意事项

    pandas.DataFrame 的 iloc # ------------------------------------------------------------ 'python式的切片,包 ...

  2. 日常记录-Pandas Cookbook

    Cookbook 1.更新内容 2.关于安装 3.Pandas使用注意事项 4.包环境 5.10分钟Pandas初识 6.教程 7.Cookbook 8.数据结构简介 9.基本功能 10.使用文本数据 ...

  3. Pandas注意事项&窍门

    警告和疑难意味着一个看不见的问题.在使用Pandas过程中,需要特别注意的地方. 与Pandas一起使用If/Truth语句 当尝试将某些东西转换成布尔值时,Pandas遵循了一个错误的惯例. 这种情 ...

  4. python2.7版本win7 64位系统安装pandas注意事项_20161226

    经过卸载安装python几经折腾,参考了各种网站,终于安装成功. [成功的步骤] 保存这个python第三方库网站,网址是http://www.lfd.uci.edu/~gohlke/pythonli ...

  5. 2018.03.27 pandas duplicated 和 replace 使用

    #.duplicated / .replace import numpy as np import pandas as pd s = pd.Series([1,1,1,1,1,2,3,3,3,4,4, ...

  6. 2018.03.27 pandas concat 和 combin_first使用

    # 连接和修补concat.combine_first 沿轴的堆叠连接 # 连接concatimport pandas as pdimport numpy as np s1 = pd.Series([ ...

  7. 2018.03.27 python pandas merge join 使用

    #2.16 合并 merge-join import numpy as np import pandas as pd df1 = pd.DataFrame({'key1':['k0','k1','k2 ...

  8. SQL查询效率注意事项 2011.12.27

    一.查询条件精确,针对有参数传入情况 二.SQL逻辑执行顺序 FROM-->JOIN-->WHERE-->GROUP-->HAVING-->DISTINCT-->O ...

  9. 27个Jupyter Notebook使用技巧及快捷键(翻译版)

    Jupyter Notebook Jupyter Notebook 以前被称为IPython notebook.Jupyter Notebook是一款能集各种分析包括代码.图片.注释.公式及自己画的图 ...

随机推荐

  1. 【2019-08-29】让自己着眼当下,真TM不容易

    07:50 天是蓝色的,路面是灰色的,树是绿色的,那个奶茶店的招牌是白色的.我的表情是木讷的,老婆的表情是嫌弃的,那个路人的表情是无解的,地铁工作人员的表情是无奈的刚才回到公司看到那个通宵了的同事的表 ...

  2. luogu P1533 可怜的狗狗 |莫队+二分

    题目背景 小卡由于公务需要出差,将新家中的狗狗们托付给朋友嘉嘉,但是嘉嘉是一个很懒的人,他才没那么多时间帮小卡喂狗狗. 题目描述 小卡家有N只狗,由于品种.年龄不同,每一只狗都有一个不同的漂亮值.漂亮 ...

  3. Prometheus 监控Docker服务器及Granfanna可视化

    Prometheus 监控Docker服务器及Granfanna可视化 cAdvisor(Container Advisor)用于收集正在运行的容器资源使用和性能信息. 使用Prometheus监控c ...

  4. Linux ip Command

    Syntax ip OBJECT COMMAND ip [options] OBJECT COMMAND ip OBJECT help Understanding ip command OBJECTS ...

  5. 《STL源码剖析》——Array

    array array本身内容较少,日常使用也不是很多,里面也没有很高深的技巧 1 array的基本架构 了解array的架构需要一个额外的语法知识: int a[100]; int [100]b; ...

  6. 在IIS下发布.Net Core MVC项目

    1. 默认你已经安装了IIS,并且创建了一个.Net Core 项目 2. 发布.NET Core项目 在vs中右键点击MVC项目,点击"发布"按钮,选择"文件系统&qu ...

  7. Bootstrap4后台导航栏制作

    <!Doctype html> <html lang="zh-cn"> <head> <!-- Required meta tags -- ...

  8. linux系统shell基础知识入门

    什么是shell shell就是我们常说的命令行程序,它是一个作为用户与Linux系统间接口的程序,它允许用户向操作系统输入要执行的命令.在Linux中安装多个shell是可行的,一般系统有默认的sh ...

  9. linux教程:[3]配置Zookeeper开机启动

    ZooKeeper是Hadoop的正式子项目: Hadoop是一个分布式系统基础架构,由Apache基金会所开发: Zookeeper能够用来leader选举:也就是你有N+1台同样的服务器的时候又z ...

  10. javascript获取url中的参数值

    javascript的实现代码如下: function QueryString(fieldName) { var urlString = document.location.search; if(ur ...