Python
pandas
isnull

Pandasで NaN (Null) の行だけ抽出

Pandasで列が NaN の行を抽出する方法

データの準備
In [3]: df=pd.DataFrame(np.random.randn(5,5))
In [4]: df.columns=list('ABCDE')
iloc で Null を設定して・・・
In [21]: df.iloc[1,1]=np.nan

In [30]: df
Out[30]:
          A         B         C         D         E
0       NaN -0.785578 -0.688584 -0.335229 -1.981495
1  0.094032       NaN  1.482541  0.160781  0.106237
2 -1.912919  1.432783       NaN       NaN  1.212469
3 -0.549426  0.236417  0.087149       NaN  1.089239
4 -0.056149 -0.696856  1.137570  0.855886       NaN

#####isnull() == True で NaN を持つ行だけを抽出できる

isnull() で NaN を持つ行だけを抽出できる

(2017/11/13 修正しました。Hoxo_m さんご指摘ありがとうございます)

In [31]: df[df['A'].isnull()]
Out[31]:
    A         B         C         D         E
0 NaN -0.785578 -0.688584 -0.335229 -1.981495

In [32]: df[df['B'].isnull()]
Out[32]:
          A   B         C         D         E
1  0.094032 NaN  1.482541  0.160781  0.106237

In [33]: df[df['C'].isnull()]
Out[33]:
          A         B   C   D         E
2 -1.912919  1.432783 NaN NaN  1.212469

In [34]: df[df['D'].isnull()]
Out[34]:
          A         B         C   D         E
2 -1.912919  1.432783       NaN NaN  1.212469
3 -0.549426  0.236417  0.087149 NaN  1.089239

In [35]: df[df['E'].isnull()]
Out[35]:
          A         B        C         D   E
4 -0.056149 -0.696856  1.13757  0.855886 NaN