TestGuest
TestGuest

Reputation: 603

Finding the total number of non-NAN elements in pandas dataframe

I know the df.count() and df.groupby.count(), but I simply want the number of non-NAN elements for a specific column (say called 'cars') of my dataframe.

I know df.size[0], but this command does not respect the fact that the number of non-NAN elements might differ in different columns.

Upvotes: 1

Views: 2809

Answers (2)

ansev
ansev

Reputation: 30920

We can also use Series.notna + Series.sum

df.cars.notna().sum()

Upvotes: 1

Andy L.
Andy L.

Reputation: 25249

There is also Series count. It also ignores NaN. From the docs

s = pd.Series([0.0, 1.0, np.nan])
s.count()

Out[307]: 2

So, for column cars

df['cars'].count()

will return number of Non-NaN values of column cars

Upvotes: 4

Related Questions