Editor

Basic GroupBy Sum

Intermediate · Write the code · from Pandas GroupBy: Split-Apply-Combine for Powerful Aggregations

From the lesson: Pandas GroupBy: Split-Apply-Combine for Powerful Aggregations

Imagine you have a giant pile of receipts from a store. You want to know how much each department earned last month. What do you do? You sort the receipts into piles by department, add up each pile, and write down the totals.

Read the lesson

Given a DataFrame df with columns department and salary, group by department and print the total salary for each department. The DataFrame is already created for you.

Print the result of the groupby sum (it should be a Series).

import pandas as pd
df = pd.DataFrame({
'department': ['Sales', 'Engineering', 'Sales', 'Engineering', 'Sales'],
'salary': [50000, 80000, 55000, 90000, 60000]
})
# Group by department, sum the salaries, and print

More Pandas GroupBy exercises

All Python exercises · 50 practice problems · Data Science & Machine Learning quiz