user9191983
user9191983

Reputation: 595

how can I get the index of rows having null values in all columns

I'd like to get index of rows which have only null values straight in pandas, python3. thanks.

Upvotes: 2

Views: 1262

Answers (1)

jezrael
jezrael

Reputation: 863166

Use:

i = df.index[df.isna().all(axis=1)]

If large DataFrame, slowier solution:

i = df[df.isna().all(axis=1)].index

Sample:

df=pd.DataFrame({"a":[np.nan,0,1], 
                 "b":[np.nan,1,np.nan]}) 

print (df)
     a    b
0  NaN  NaN
1  0.0  1.0
2  1.0  NaN

i = df.index[df.isna().all(axis=1)]
print (i)
Int64Index([0], dtype='int64')

Explanation:

First compare missing values by DataFrame.isna:

print (df.isna())
       a      b
0   True   True
1  False  False
2  False   True

Then check if all Trues per rows by DataFrame.all:

print (df.isna().all(axis=1))
0     True
1    False
2    False
dtype: bool

And last filter index values by boolean indexing.

Upvotes: 2

Related Questions