Home > database >  How to get index number of pandas data frame that contain only null values
How to get index number of pandas data frame that contain only null values

Time:02-05

Consider the below data frame.

Dataframe

I want to extract the ids of this dataframe that contain only null values. For example, id 1 has only null values. So the answer should be index 1. Can you please explain how to extract this?

Please note that I do not want ids that contain partial null values, for instance, IDs 0 and 1.

CodePudding user response:

Use isnull with all :

m = df.isnull().all(axis=1)

L = list(df[m].index)

print(L)
#[1]

CodePudding user response:

You can use like this:

null_row = df[df.isnull().all(axis=1)].index
  1. isnull() is function for the empty values
  2. all is looks all things depend the parameter(check the below)
  3. axis=1 refers to the columns
  4. index is for getting index number

CodePudding user response:

You can use .all to find all rows and .any to find any rows (where axis=1 gives you rows).

df = pd.DataFrame(
    {
        "Clm1": ['A', np.nan, 'C', np.nan],
        "Clm2": [20.0, np.nan, 19.0, np.nan],
        "Clm3": [np.nan, np.nan, 'X', 'Y'],
    }
)

ans = df[df.isna().all(axis=1)]
print(list(ans.index))
[1]
  • Related