Rahul
Rahul

Reputation: 129

How to get null counts of each rows except one column?

df = pd.DataFrame({'Last_Name': ['Smith', Null, Null,'Joy'], 
               'First_Name': ['John', Null, 'Bill', 'Tony'],
               'Age': [35, 45, Null, 60]})

How can I get the null counts of only First_Name and Age and not Last_Name? I am doing something like below but it is fetching all null counts of all columns.

null_counts = irs_df.isnull().sum()
null_counts[null_counts > 0].sort_values(ascending=False)

How to filter out Last_Name?

Upvotes: 1

Views: 77

Answers (3)

piRSquared
piRSquared

Reputation: 294288

count

Counts the non-nulls. So subtract it from the length of the dataframe

(len(df) - df.count()).drop('Last_Name')

First_Name    1
Age           1
dtype: int64

Upvotes: 0

David Buck
David Buck

Reputation: 3828

An alternative method to df.drop would be to use DataFrame.isin on the columns to remove the column that isn't of interest:

null_counts = df.loc[:, ~df.columns.isin(['Last_Name'])].isnull().sum().sort_values(ascending=False)
print (null_counts)
Age           1
First_Name    1
dtype: int64

A quick comparison of timings between drop and isin approaches on a much larger dataframe suggests there's not a lot in it.

Upvotes: 2

jezrael
jezrael

Reputation: 862691

You can select all columns without first by DataFrame.iloc:

null_counts = irs_df.iloc[:, 1:].isnull().sum()
print (null_counts)
First_Name    1
Age           1
dtype: int64

Or remove column by DataFrame.drop:

null_counts = irs_df.drop('Last_Name', axis=1).isnull().sum()
print (null_counts)
First_Name    1
Age           1
dtype: int64

Upvotes: 6

Related Questions