Python pandas 列の範囲を指定して欠損値判定
# Make DataframeIn[1]:df = pd.DataFrame({"A" : ['foo', np.nan, 'bar', np.nan,'foo', 'bar', 'foo', 'foo'],"B" : ['one', 'one', np.nan, np.nan,'two', 'two', 'one', 'three'],"C" : ['hoge', 'fuga', np.nan, 'fuga', np.nan, np.nan, 'hoge', 'fuga'],"D" : np.random.randn(8)})Out[2]:A B C D0 foo one hoge -0.6507221 NaN one fuga 1.3431462 bar NaN NaN -0.5609933 NaN NaN fuga 0.1369374 foo two NaN 0.4613155 bar two NaN -0.1728286 foo one hoge -1.4390347 foo three fuga -1.908443In[3]:df.ix[:,:2].isnull().any(axis=1)Out[4]:0 False1 True2 True3 True4 False5 False6 False7 Falsedtype: boolこの方法の欠点:スライスしたDataFrameがSeriesの場合はエラーが出る。
In[4]:df.ix[:,1].isnull().any(axis=1)Traceback (most recent call last):File "", line 1, indf.ix[:,1].isnull().any(axis=1)File "/usr/lib/python3/dist-packages/pandas/core/generic.py", line 4913, in logical_funcname=name)File "/usr/lib/python3/dist-packages/pandas/core/series.py", line 2189, in _reduceself._get_axis_number(axis)File "/usr/lib/python3/dist-packages/pandas/core/generic.py", line 315, in _get_axis_number.format(axis, type(self)))ValueError: No axis named 1 for object type