2017-04-09 18 views
1

看來,在熊貓,你可以做任何的那些:什麼時候調用object.method與Python/Pandas中的module.function?

age_is_null = pd.isnull(titanic_survival["age"]) 
age_is_null = titanic_survival["age"].isnull() 

似乎都存在:在熊貓模塊的功能,並在數據框類中的方法(在其他模塊)。

來自Obj-C背景,這是令人困惑的。爲什麼需要這兩個?

+0

相關:[?爲什麼numpy的有許多ndarray方法相應的功能(http://stackoverflow.com/questions/29120730/why-does-numpy-have -a-相應功能換許多-ndarray的方法) – ayhan

回答

1

pd.isnull與工程(東西都迭代器)e.g

>>> import pandas as pd 
>>> import numpy as np 
>>> pd.isnull(np.array([1, 2])) 
array([False, False], dtype=bool) 
>>> pd.isnull([1, 2]) 
array([False, False], dtype=bool) 

df.isnull是綁定到你的數據框對象的成員函數不同類型的輸入。因此,只要首先創建DataFrame成本高昂,就可以使用pd.isnull

時序:

In [30]: %timeit pd.isnull([1,2]) 
The slowest run took 8.93 times longer than the fastest. This could mean that an intermediate result is being cached. 
100000 loops, best of 3: 9.19 µs per loop 

In [33]: %timeit pd.DataFrame([1,2]).isnull() 
The slowest run took 6.42 times longer than the fastest. This could mean that an intermediate result is being cached. 
1000 loops, best of 3: 202 µs per loop 
相關問題