Finding the total number of non-NAN elements in pandas dataframe
NickName:TestGuest Ask DateTime:2019-11-05T07:07:09

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.

Copyright Notice:Content Author:「TestGuest」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/58702316/finding-the-total-number-of-non-nan-elements-in-pandas-dataframe

Answers
Andy L. 2019-11-04T23:12:20

There is also Series count. It also ignores NaN. From the docs\n\ns = pd.Series([0.0, 1.0, np.nan])\ns.count()\n\nOut[307]: 2\n\n\nSo, for column cars\n\ndf['cars'].count()\n\n\nwill return number of Non-NaN values of column cars",


More about “Finding the total number of non-NAN elements in pandas dataframe” related questions

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

Show Detail

Keep indices in Pandas DataFrame with a certain number of non-NaN entires

Lets say I have the following dataframe: df1 = pd.DataFrame(data = [1,np.nan,np.nan,1,1,np.nan,1,1,1], columns = ['X'], index = ['a', 'a', 'a', ..

Show Detail

count total number of list elements in pandas column

I have a pandas dataframe A with column keywords as (here Im showing only 4 rows but in actual there are millions) :- keywords ['loans','mercedez','bugatti'] ['trump','usa'] [

Show Detail

Retain indices in Dask DataFrame with a certain number of non-NaN entires

Lets say I construct the following DataFrame in Dask: import pandas as pd import dask.dataframe as dd pdf = pd.DataFrame(data = [1,np.nan,np.nan,1,1,np.nan,1,1,1], columns = ...

Show Detail

select non-NaN rows with multiple conditions from a pandas dataframe

Assume there is a dataframe such as import pandas as pd import numpy as np df = pd.DataFrame({'col1':[1,2,3,4,5], 'col2':[11,12,np.nan,24,np.nan]}) df col1 col2 0 1 ..

Show Detail

How can I get the index of next non-NaN number with series in pandas?

In pandas, I am now looping with an instance of Series, is it possible for me to know the index of the next non-NaN instantly when I meet a NaN. I don't want to skip those NaNs, because I want to d...

Show Detail

How to get row, column indices of all non-NaN items in Pandas dataframe

How do I iterate over a dataframe like the following and return the non-NaN value locations as a tuple. i.e. df: 0 1 2 0 NaN NaN 1 1 1 NaN NaN 2 NaN 2 NaN I would get...

Show Detail

Get row-index of the last non-NaN value in each column of a pandas data frame

How can I return the row index location of the last non-nan value for each column of the pandas data frame and return the locations as a pandas dataframe?

Show Detail

How to extract all non-nan values in this dataframe by the non-nan values count of each row

I want to extract the non-nan values in each row of dataframe. I get a dataframe whose number of rows is more than 3000 as follows: # this is just an example, not whole data l1 l2 l3 0 2...

Show Detail

Total length of elements, and subsets, in a pandas dataframe

How I can count the total elements in a dataframe, including the subset, and put the result in the new column? import pandas as pd x = pd.Series([[1, (2,5,6)], [2, (3,4)], [3, 4], [(5,6), (7,8,9)]...

Show Detail