2017-04-09 3 views
1

パンダにあなたがそれらのいずれかを行うことができますようだ:パンダモジュール内の関数、および(別のモジュールで)データフレームクラスのメソッドを:Python/Pandasでは、object.methodとmodule.functionをいつ呼び出すのですか?

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

両方が存在するようです。

Obj-Cバックグラウンドから来ている、これは混乱しています。なぜ両方の必要性?異なる種類の入力で

+0

関連:[なぜ、多くのndarray方法のために、対応する機能を持ってnumpyのん?](http://stackoverflow.com/questions/29120730/why-does-numpy-have -a対応関数のための多数のndarray法) – ayhan

答えて

1

pd.isnull作品(反復可能であるもの)例えば

>>> 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 
関連する問題