>>>Task
Datasets usually arrive long: one row per event, many rows per thing you care about. Six temperature readings, three cities. Group by turns that into one row per city.
readings.groupby("city")["temperature"].mean()Read it left to right:
groupby("city") — put the rows into piles, one pile per distinct city["temperature"] — from each pile, take that column.mean() — reduce each pile to a single numberWhat comes back is a Series indexed by city:
city
Cairo 35.000000
Lisbon 22.666667
Oslo 5.500000
Name: temperature, dtype: float64Because the index is the city name, you look values up by name — by_city["Lisbon"] — and
.idxmax() gives you the label of the largest value rather than its position.
.mean() is only one option: .sum(), .max(), .count() and .min() all work the
same way.
readings — a DataFrame with the columns city and temperatureby_city — the average temperature per city, using groupbywarmest — the name of the warmest city, as a strlisbon_average — Lisbon's average, as a floatWarmest: Cairo
Lisbon averages 22.67The solution unlocks after a few minutes.
Run your code to see the output.