Python/Pandas: counting the number of missing/NaN in each row

前端 未结 1 387
灰色年华
灰色年华 2020-12-13 03:44

I\'ve got a dataset with a big number of rows. Some of the values are NaN, like this:

In [91]: df
Out[91]:
 1    3      1      1      1
 1    3      1      1         


        
相关标签:
1条回答
  • 2020-12-13 04:28

    You could first find if element is NaN or not by isnull() and then take row-wise sum(axis=1)

    In [195]: df.isnull().sum(axis=1)
    Out[195]:
    0    0
    1    0
    2    0
    3    3
    4    0
    5    0
    dtype: int64
    

    And, if you want the output as list, you can

    In [196]: df.isnull().sum(axis=1).tolist()
    Out[196]: [0, 0, 0, 3, 0, 0]
    

    Or use count like

    In [130]: df.shape[1] - df.count(axis=1)
    Out[130]:
    0    0
    1    0
    2    0
    3    3
    4    0
    5    0
    dtype: int64
    
    0 讨论(0)
提交回复
热议问题