<Python, pandas> NoneTypeの判定
NoneType
の判定。
In [66]: n = None In [67]: n In [68]: type(n) Out[68]: NoneType In [69]: isinstance(n, NoneType) --------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-69-d5714e961eee> in <module>() ----> 1 isinstance(n, NoneType) NameError: name 'NoneType' is not defined In [70]: isinstance(n, type(n)) Out[70]: True
へー、、確かに、NoneType
直接渡したら怒られる。。。へんなの。。。
こちらで勉強させてもらいました。
ただ、from types import NoneType
はできなかった。
NoneType
がないと怒られた、、、
応用。
じゃ、pandas
さんでの場合。
In [72]: df Out[72]: a b 0 1 None 1 2 NaN 2 3 5 3 NaN 8 4 4 a 5 5 9 6 NaN 8 7 6 None In [73]: df.ix[0,'b'] In [74]: type(df.ix[0,'b']) Out[74]: NoneType In [75]: df.apply(lambda x: isinstance(x[0], type(None)), axis=1) Out[75]: 0 False 1 False 2 False 3 False 4 False 5 False 6 False 7 False dtype: bool In [76]: df.apply(lambda x: isinstance(x[1], type(None)), axis=1) Out[76]: 0 True 1 False 2 False 3 False 4 False 5 False 6 False 7 True dtype: bool
各要素element
単位では、
In [82]: df.applymap(lambda x: isinstance(x, type(None))) Out[82]: a b 0 False True 1 False False 2 False False 3 False False 4 False False 5 False False 6 False False 7 False True
にゃるほど。