The count()
function is used to take count of non-NA (None, NaN, NaT values) cells present in a column or row or a DataFrame object.
DataFrame.count(axis=0, level=None, numeric_only=False)
axis
: This takes either 0
(or index
) or 1
(or columns
) for the counts to be generated for either the columns
or the rows
, respectively.level
: This takes a string level name to take count along with the given level.numeric_only
: This takes a boolean value indicating if only float, integer, or boolean values should be included.This function returns a DataFrame showing the number of non-null entries in it.
# A code to illustrate the count() function in Pandas # importing the pandas and numpy libraries import pandas as pd import numpy as np # creating a dataframe df = pd.DataFrame({"Person": ["John", "Myla", "Lewis", "John", "Myla"], "Age": [24., np.nan, 21., 33, 26], "Single": [False, True, True, True, False]}) # printing the dataframe object print(df) # taking count of the non-NA values in present in the columns print(" This is a dataframe of each columns and their Number of non-NA values: ", df.count()) # taking the count of the non-NA values in each of the rows print("These are the number of non-NA values in each of the rows: ", df.count(axis=1))
df
.df
.count()
function.count()
function.RELATED TAGS
CONTRIBUTOR
View all Courses