Python Pandas 警告和陷阱
警告意味着警告,陷阱意味着看不见的问题。
在 Pandas 中使用 If/Truth 语句
当你尝试将某些内容转换为 bool .这发生在一个 if or when 使用布尔运算,并且, or , or not .目前尚不清楚结果应该是什么。它应该是真的,因为它不是零长度吗? False 因为有 False 值?目前还不清楚,因此,Pandas 提出了一个 值错误 :
import pandas as pd if pd.Series([False, True, False]): print 'I am True'
Its output 如下:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool() a.item(),a.any() or a.all().
In if 状况,目前尚不清楚如何处理它。该错误提示是否使用 None or 其中任何一个 .
import pandas as pd if pd.Series([False, True, False]).any(): print("I am any")
Its output 如下:
I am any
要在布尔上下文中评估单元素 pandas 对象,请使用方法 .bool() :
import pandas as pd print pd.Series([True]).bool()
Its output 如下:
True
按位布尔值
位布尔运算符,例如 == 和 ! = 将返回一个布尔系列,无论如何这几乎总是需要的。
import pandas as pd s = pd.Series(range(5)) print s==4
Its output 如下:
0 False 1 False 2 False 3 False 4 True dtype: bool
伊辛操作
这将返回一个布尔系列,显示系列中的每个元素是否完全包含在传递的值序列中。
import pandas as pd s = pd.Series(list('abc')) s = s.isin(['a', 'c', 'e']) print s
Its output 如下:
0 True 1 False 2 True dtype: bool
重新索引 vs ix Gotcha
许多用户会发现自己在使用 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 df.ix[['b', 'c', 'e']]
Its output 如下:
one two three four a -1.582025 1.335773 0.961417 -1.272084 b 1.461512 0.111372 -0.072225 0.553058 c -1.240671 0.762185 1.511936 -0.630920 d -2.380648 -0.029981 0.196489 0.531714 e 1.846746 0.148149 0.275398 -0.244559 f -1.842662 -0.933195 2.303949 0.677641 one two three four b 1.461512 0.111372 -0.072225 0.553058 c -1.240671 0.762185 1.511936 -0.630920 e 1.846746 0.148149 0.275398 -0.244559
当然,在这种情况下,这完全等同于使用 reindex method:
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 df.reindex(['b', 'c', 'e'])
Its output 如下:
one two three four a 1.639081 1.369838 0.261287 -1.662003 b -0.173359 0.242447 -0.494384 0.346882 c -0.106411 0.623568 0.282401 -0.916361 d -1.078791 -0.612607 -0.897289 -1.146893 e 0.465215 1.552873 -1.841959 0.329404 f 0.966022 -0.190077 1.324247 0.678064 one two three four b -0.173359 0.242447 -0.494384 0.346882 c -0.106411 0.623568 0.282401 -0.916361 e 0.465215 1.552873 -1.841959 0.329404
有些人可能会得出这样的结论 ix and reindex 在此基础上是 100% 等效的。除非在整数索引的情况下,否则这是正确的。例如,上面的操作也可以表示为:
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 df.ix[[1, 2, 4]] print df.reindex([1, 2, 4])
Its output 如下:
one two three four a -1.015695 -0.553847 1.106235 -0.784460 b -0.527398 -0.518198 -0.710546 -0.512036 c -0.842803 -1.050374 0.787146 0.205147 d -1.238016 -0.749554 -0.547470 -0.029045 e -0.056788 1.063999 -0.767220 0.212476 f 1.139714 0.036159 0.201912 0.710119 one two three four b -0.527398 -0.518198 -0.710546 -0.512036 c -0.842803 -1.050374 0.787146 0.205147 e -0.056788 1.063999 -0.767220 0.212476 one two three four 1 NaN NaN NaN NaN 2 NaN NaN NaN NaN 4 NaN NaN NaN NaN
重要的是要记住 reindex 只是严格的标签索引 .在索引包含整数和字符串的病理情况下,这可能会导致一些潜在的令人惊讶的结果。