Counting the number of missing/NaN in each row
Asked Answered
K

2

93

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
 2    3      1      1      1
 1    1    NaN    NaN    NaN
 1    3      1      1      1
 1    1      1      1      1

And I want to count the number of NaN values in each row, it would be like this:

In [91]: list = <somecode with df>
In [92]: list
    Out[91]:
     [0,
      0,
      0,
      3,
      0,
      0]

What is the best and fastest way to do it?

Karankaras answered 5/5, 2015 at 17:14 Comment(1)
Similar question for columns: How do I get a summary count of missing/NaN data by column in 'pandas'?Oxblood
S
135

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
Snuffbox answered 5/5, 2015 at 17:15 Comment(0)
P
2

To count NaNs in specific rows, use

cols = ['col1', 'col2']
df['number_of_NaNs'] = df[cols].isna().sum(1)

or index the columns by position, e.g. count NaNs in the first 4 columns:

df['number_of_NaNs'] = df.iloc[:, :4].isna().sum(1)
Pegues answered 28/1, 2023 at 7:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.