W3docs

Pandas DataFrame Groupby two columns and get counts

Here is an example code snippet that demonstrates how to use the groupby() method in pandas to group a DataFrame by two columns and get the counts for each group:

Here is an example code snippet that demonstrates how to use the groupby() method in pandas to group a DataFrame by two columns and get the counts for each group:

Use the groupby() method in pandas to group a DataFrame by two columns and get the counts for each group

import pandas as pd

# Create a sample DataFrame
df = pd.DataFrame({'A': ['foo', 'bar', 'foo', 'bar',
                         'foo', 'bar', 'foo', 'foo'],
                   'B': ['one', 'one', 'two', 'three',
                         'two', 'two', 'one', 'three'],
                   'C': [1, 2, 3, 4, 5, 6, 7, 8],
                   'D': [10, 20, 30, 40, 50, 60, 70, 80]})

# Group the DataFrame by columns 'A' and 'B'
grouped = df.groupby(['A', 'B']).size().reset_index(name='Counts')

# Print the resulting DataFrame
print(grouped)

<div class="alert alert-info flex not-prose"> Watch a course <span class="hidden md:block">Watch a video course </span> Python - The Practical Guide</div>

This will group the DataFrame by columns 'A' and 'B', and return a new DataFrame with the counts for each group. The resulting DataFrame will look like this:

resulting DataFrame will look like this

A      B  Counts
0  bar    one       1
1  bar  three       1
2  bar    two       1
3  foo    one       2
4  foo  three       1
5  foo    two       2

You can also use .count() instead of .size(). Note that .count() returns a DataFrame with column-wise counts of non-NA/null values, whereas .size() returns a single Series of total row counts per group (including NaNs).