Pandas DataFrame - count() function
The Pandas DataFrame - count() function is used to count non-NA cells for each column or row. The values None, NaN, NaT, and optionally pandas.inf (depending on pandas.options.mode.use_inf_as_na) are considered NA.
Syntax
DataFrame.count(axis=0, level=None, numeric_only=False)
Parameters
axis |
Optional. Specify {0 or 'index', 1 or 'columns'}. If 0 or 'index' counts are generated for each column. If 1 or 'columns' counts are generated for each row. Default: 0 |
level |
Optional. Specify level (int or str). If the axis is a MultiIndex (hierarchical), count along a particular level, collapsing into a DataFrame. A str specifies the level name. |
numeric_only |
Optional. Specify True to include only float, int or boolean data. Default: False |
Return Value
Returns a Series of count of non-NA/null entries for each column/row. If level is specified DataFrame is returned.
Example: Using count() column-wise on whole DataFrame
In the below example, a DataFrame info is created. The DataFrame count() function is used to get the count of each column which only includes only non-NA values.
import pandas as pd import numpy as np info = pd.DataFrame({ "Person": ["John", "Mary", "Jo", "Sam"], "Age": [25, 24, 30, 28], "Bonus": ["10K", np.nan, "10K", "9K"] }) print(info) print(info.count())
The output of the above code will be:
Age Bonus Person 0 25 10K John 1 24 NaN Mary 2 30 10K Jo 3 28 9K Sam Age 4 Bonus 3 Person 4 dtype: int64
Example: Using count() row-wise on whole DataFrame
To get the row-wise count, the axis parameter can set to 1.
import pandas as pd import numpy as np info = pd.DataFrame({ "Person": ["John", "Mary", "Jo", "Sam"], "Age": [25, 24, 30, 28], "Bonus": ["10K", np.nan, "10K", "9K"] }) print(info) print(info.count(axis=1))
The output of the above code will be:
Age Bonus Person 0 25 10K John 1 24 NaN Mary 2 30 10K Jo 3 28 9K Sam 0 3 1 2 2 3 3 3 dtype: int64
Example: Using count() on selected column
Instead of whole data frame, the count() function can be applied on selected columns. Consider the following example.
import pandas as pd import numpy as np info = pd.DataFrame({ "Person": ["John", "Mary", "Jo", "Sam"], "Age": [25, 24, 30, 28], "Bonus": ["10K", np.nan, "10K", "9K"] }) print(info) #count on single column print("\ncount on Person returns:") print(info['Person'].count()) #count on multiple columns print("\ncount on Person and Bonus returns:") print(info[['Person', 'Bonus']].count())
The output of the above code will be:
Age Bonus Person 0 25 10K John 1 24 NaN Mary 2 30 10K Jo 3 28 9K Sam count on Person returns: 4 count on Person and Bonus returns: Person 4 Bonus 3 dtype: int64
Example: Using count() with level parameter
The below example shows how to create one level of a MultiIndex.
import pandas as pd import numpy as np info = pd.DataFrame({ "Person": ["John", "Mary", "Jo", "Sam"], "Age": [25, 24, 30, 28], "Bonus": ["10K", np.nan, "10K", "9K"] }) print(info) #count with level parameter print(info.set_index(['Person', 'Bonus']).count(level='Person'))
The output of the above code will be:
Age Bonus Person 0 25 10K John 1 24 NaN Mary 2 30 10K Jo 3 28 9K Sam Age Person Jo 1 John 1 Mary 1 Sam 1
❮ Pandas DataFrame - Functions