Consider the below data frame.
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:
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
- isnull() is function for the empty values
- all is looks all things depend the parameter(check the below)
- axis=1 refers to the columns
- 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]