45
29

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Last updated at Posted at 2017-11-13

#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

45
29
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
45
29

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?