Reference Materials
Certification Courses
Created with over a decade of experience and thousands of feedback.
Pandas count()
In this tutorial, we will learn about the count() method in Pandas with the help of examples.
The count() method in Pandas is used to count non-missing values across the given axis.
Example
import pandas as pd
# sample DataFrame
data = {
'A': [1, 2, 3, None],
'B': [None, 5, None, 8],
'C': [9, 10, 11, 12]
}
df = pd.DataFrame(data)
# use count() method
print(df.count())
'''
Output
A 3
B 2
C 4
dtype: int64
'''
count() Syntax
The syntax of the count() method in Pandas is:
df.count(axis=0, numeric_only=False)
count() Arguments
The count() method takes following arguments:
axis(optional) - specifies if we want to count non-missing values by rows or columnsnumeric_only(optional) - ifTrue, it will include only float, int, boolean columns. IfFalse, it will count all columns including object type.
count() Return Value
The count() method returns the number of non-missing values for the specified axis.
Example1: Count Non-Missing Values Along Columns
import pandas as pd
# create a sample dataframe
data = {
'A': [1, 2, 4, 4],
'B': ['a', None, 'c', None],
'C': [1.1, None, 3.3, 4.4]
}
df = pd.DataFrame(data)
# count non-missing values along columns
non_missing_counts = df.count()
print(non_missing_counts)
Output
A 4 B 2 C 3 dtype: int64
In the above example, we have used the count() method to count non-missing values along columns.
The displayed output shows the number of non-missing values in each column:
- Column
Ahas 4 non-missing values. - Column
Bhas 2 non-missing values. - Column
Chas 3 non-missing values.
Example 2: Count Non-Missing Values Along Rows
import pandas as pd
# create a sample dataframe
data = {
'A': [1, 2, 4, 4],
'B': ['a', None, 'c', None],
'C': [1.1, None, 3.3, 4.4]
}
df = pd.DataFrame(data)
# count non-missing values along rows
non_missing_counts_rows = df.count(axis=1)
print(non_missing_counts_rows)
Output
0 3 1 1 2 3 3 2 dtype: int64
In this example, we have specified axis=1 inside count() to count non-missing values along each row.
Here,
- Row 0 has 3 valid values:
[1, 'a', 1.1]. - Row 1 has 1 valid value:
[2]. Both the other entries areNone. - Row 2 has 3 valid values:
[4, 'c', 3.3]. - Row 3 has 2 valid values:
[4, 4.4], with one entry asNone.
Example 3: Count Only Numeric Columns
import pandas as pd
# create a sample dataframe with mixed data types
data = {
'A': [1, 2, 4, 4],
'B': ['a', None, 'c', None],
'C': [1.1, None, 3.3, 4.4],
'D': [True, False, None, True]
}
df = pd.DataFrame(data)
# count non-missing values for only numeric columns
non_missing_counts_numeric = df.count(numeric_only=True)
print(non_missing_counts_numeric)
Output
A 4 C 3 dtype: int64
Here, we used the count(numeric_only=True) method on the df DataFrame. The method counts non-missing values, but it only considers numeric columns.
The output shows the count of non-missing values for only the numeric columns:
- Column
'A'has 4 non-missing values. - Column
'C'has 3 non-missing values.