Difference between size and count in Pandas?
Last Updated :
28 Apr, 2025
When dealing with data frames, two commonly used methods are size() and count(). While they might seem similar at first glance, they serve different purposes and produce different results. In this article, we'll explore the What's the differences between size() and count() in Pandas and when to use each of them.
Difference between the size and count of Pandas?
Let's see some of the key differences between size() and count() in Pandas.
Inclusion of NaN Values
- size() counts all elements, including NaN values.
- count() counts only non-null (valid) values, excluding NaN values.
Result Type
- size() returns a single integer representing the total number of elements.
- count() returns a Series with the count of non-null values for each column (if applied to a DataFrame) or a single integer (if applied to a Series).
When to Use size() or count() Methods
Knowing when to use size() or count() depends on your specific data analysis needs:
- Use Pandas size() when you want to understand the overall size of your dataset, including missing values. For example, you might use it to calculate proportions or ratios involving missing data.
- Use Pandas count() when you need to know how many valid data points you have in a specific column or when you want to filter out rows with missing values for further analysis.
What is the size of Pandas?
We use size() as the number of rows in a group returned as length. There is no differentiation between nan and non-null values.
Using size() with a Pandas Series
In this example, the size() method is applied to a Series, and it counts all elements, including the NaN value. The result is 5.
Python3
import pandas as pd
data = {'A': [1, 2, 3, None, 5]}
series = pd.Series(data['A'])
total_size = series.size
print(total_size)
Output:
5
Using size() with filtering
In this example, we first filter the DataFrame to include only rows where column 'A' is not null. Then, we use size() to count all elements in the filtered DataFrame, resulting in a size of 4.
Python3
import pandas as pd
data = {'A': [1, 2, 3, None, 5]}
df = pd.DataFrame(data)
# Filtering rows where column 'A' is not null
filtered_df = df[df['A'].notnull()]
filtered_size = filtered_df.size
print(filtered_df)
print(filtered_size)
Output
A
0 1.0
1 2.0
2 3.0
4 5.0
4
What is count in Pandas?
In count() it generally count the non -null values in Data frames. This method does differentiation between non-nan and nan value.
Using count() with a Pandas DataFrame
In this example, the count() method is applied to a DataFrame, and it counts the number of non-null values in each column. Both column 'A' and 'B' have 4 valid values.
Python3
import pandas as pd
data = {'A': [1, 2, 3, None, 5],
'B': [6, 7, None, 9, 10]}
df = pd.DataFrame(data)
count_valid = df.count()
print(count_valid)
Output
A 4
B 4
dtype: int64
Using count() with filtering
Here, we filter the DataFrame to include only rows where column 'A' is not null, and then we use count() on that filtered column to count the valid values. The result is 4, which represents the count of non-null values in column 'A' after filtering.
Python3
import pandas as pd
data = {'A': [1, 2, 3, None, 5]}
df = pd.DataFrame(data)
# Filtering rows where column 'A' is not null
filtered_df = df[df['A'].notnull()]
count_filtered = filtered_df['A'].count()
print(filtered_df)
print(count_filtered)
Output
A
0 1.0
1 2.0
2 3.0
4 5.0
4
Difference between size() and count() in Pandas
Code to demonstration to show the difference between size and count in Pandas.
Python3
import pandas as pd
data = {'A': [1, 2, 3, None, 5],
'B': [6, 7, 8, 9, 10]}
df = pd.DataFrame(data)
# Calculates the total number of elements in the DataFrame
total_size = df.size
# Output: 10 (5 elements in column 'A' + 5 elements in column 'B')
print(total_size)
# Counts non-null values in column 'A'
count_column_A = df['A'].count()
# Output: 4 (4 non-null values in column 'A')
print(count_column_A)
Output:
10
4
Similar Reads
Difference between Pandas and PostgreSQL Pandas: Python supports an in-built library Pandas, to perform data analysis and manipulation is a fast and efficient way. Pandas library handles data available in uni-dimensional arrays, called series, and multi-dimensional arrays called data frames. It provides a large variety of functions and uti
4 min read
Difference between Pandas VS NumPy Python is one of the most popular languages for Machine Learning, Data Analysis, and Deep learning tasks. It is powerful because of its libraries that provide the user full command over the data. Today, we will look into the most popular libraries i.e. NumPy and Pandas in Python, and then we will co
3 min read
Difference between __sizeof__() and getsizeof() method - Python We have two Python methods, __sizeof__() and sys.getsizeof(), both used to measure the memory size of an object. While they seem similar, they produce different results. For example, calling these methods on the same object may return different values. Understanding this difference is essential for
3 min read
Count distinct in Pandas aggregation In this article, let's see how we can count distinct in pandas aggregation. So to count the distinct in pandas aggregation we are going to use groupby() and agg() method. Â groupby(): This method is used to split the data into groups based on some criteria. Pandas objects can be split on any of thei
2 min read
Count number of rows and columns in Pandas dataframe In Pandas understanding number of rows and columns in a DataFrame is important for knowing structure of our dataset. Whether we're cleaning the data, performing calculations or visualizing results finding shape of the DataFrame is one of the initial steps. In this article, we'll explore various ways
3 min read