Python:Pandas size()

MamtaWardhani's avatar
Published Dec 17, 2025
Contribute to Docs

The size() method in pandas returns the number of rows or elements in each group created by the groupby() function. It provides a quick way to determine group sizes without applying an aggregation function.

  • Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.
    • Includes 27 Courses
    • With Professional Certification
    • Beginner Friendly.
      95 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours

Syntax

DataFrameGroupBy.size()

Parameters:

The size() method doesn’t take any parameters.

Return value:

The size() method returns a Series containing the size (row count) of each group created by groupby().

Example 1: Counting Rows by Group

In this example, a DataFrame of employees is grouped by their department, and size() counts how many employees belong to each department:

import pandas as pd
data = {
'Department': ['HR', 'IT', 'HR', 'Finance', 'IT', 'Finance'],
'Employee': ['John', 'Sara', 'Mike', 'Anna', 'Tom', 'Chris']
}
df = pd.DataFrame(data)
group_sizes = df.groupby('Department').size()
print(group_sizes)

The output of this code is:

Department
Finance 2
HR 2
IT 2
dtype: int64

Example 2: Using Multiple Grouping Columns

In this example, size() counts the number of members in each combination of team and shift within a dataset:

import pandas as pd
data = {
'Team': ['A', 'A', 'B', 'B', 'B', 'C'],
'Shift': ['Day', 'Night', 'Day', 'Night', 'Day', 'Day'],
'Name': ['John', 'Sara', 'Mike', 'Anna', 'Tom', 'Chris']
}
df = pd.DataFrame(data)
group_sizes = df.groupby(['Team', 'Shift']).size()
print(group_sizes)

The output of this code is:

Team Shift
A Day 1
Night 1
B Day 2
Night 1
C Day 1
dtype: int64

Codebyte Example: Counting Transactions Per Product

In this example, size() is used to count how many sales transactions occurred for each product in a store dataset:

Code
Output

Frequently Asked Questions

1. What is the pandas groupby().size() method?

groupby().size() returns the number of rows in each group created by groupby().

2. What is the purpose of groupby() in pandas?

groupby() splits data into groups based on selected column values to enable aggregation and summarization.

3. What does NaN stand for in pandas?

NaN stands for Not a Number and indicates missing or undefined data.

All contributors

Contribute to Docs

Learn Python:Pandas on Codecademy

  • Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.
    • Includes 27 Courses
    • With Professional Certification
    • Beginner Friendly.
      95 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours