Home > Software design >  How to find row number in a dataframe(data imported from a csv file) if one or few columns in a row
How to find row number in a dataframe(data imported from a csv file) if one or few columns in a row

Time:09-22

I have to find the row number from the dataframe where there is null values in specific columns. Which pandas function shall I use?

CodePudding user response:

Considering the dataframe below :

import pandas as pd
import numpy as np

data_dict = {'col1': [np.nan, 'bar', 'baz','missing'],
             'col2': ['', 'foo', np.nan, 'bar'],
             'col3': [249,np.nan,924,np.nan]
            }
df = pd.DataFrame (data_dict)

print(df)

      col1 col2   col3
0      NaN       249.0
1      bar  foo    NaN
2      baz  NaN  924.0
3  missing  bar    NaN

You can use the pandas.isnull to get your expected output :

out = df.loc[df[['col1','col2']].isnull().any(1)].index.tolist()

print(out)
[0, 2]
  • Related