Pandas | 27 注意事項&竅門

警告和疑難意味着一個看不見的問題。在使用Pandas過程當中,須要特別注意的地方。shell

與Pandas一塊兒使用If/Truth語句

當嘗試將某些東西轉換成布爾值時,Pandas遵循了一個錯誤的慣例。 這種狀況發生在使用布爾運算的。 目前還不清楚結果是什麼。 若是它是真的,由於它不是zerolength? 錯誤,由於有錯誤的值? 目前還不清楚,Pandas提出了一個ValueError -dom

import pandas as pd

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

輸出結果:spa

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

if條件,它不清楚如何處理它。錯誤提示是否使用None或任何這些。code

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())

輸出結果:blog

True
 

按位布爾值

按位布爾運算符(如==!=)將返回一個布爾系列,這幾乎老是須要的。索引

import pandas as pd

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

輸出結果:three

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

isin操做符字符串

這將返回一個布爾序列,顯示系列中的每一個元素是否徹底包含在傳遞的值序列中。pandas

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只是嚴格的標籤索引。這可能會致使一些潛在的使人驚訝的結果,例如索引包含整數和字符串的病態狀況。

相關文章
相關標籤/搜索