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:
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)
Watch a video course
Python - The Practical Guide
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:
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()
inplace of .size()
to get the count of non-NA/null values in each group.