Menu
CoddyTech

Several Summaries

Part of the Data and Plots section of Coddy's Machine Learning journey. Lesson 44 of 60.

One number per group hides the extremes. .agg takes a list of statistic names and builds one column for each:

Five screenings piled by genre, comedy and drama, then summarized into a table with a mean and a max column: comedy 97.5 and 100, drama 130.0 and 150

For those five screenings, plus .size(), which counts each group's rows:

shows = pd.DataFrame({
    "genre": ["comedy", "comedy", "drama", "drama", "drama"],
    "minutes": [95, 100, 130, 110, 150],
})
print(shows.groupby("genre")["minutes"].agg(["mean", "max"]))
print(shows.groupby("genre").size())

This prints:

         mean  max
genre             
comedy   97.5  100
drama   130.0  150
genre
comedy    2
drama     3
dtype: int64

Comedies run 97.5 minutes on average, 100 at most, and "min" or "sum" work too. Check the sizes: a mean from 2 rows is shakier than one from 200.

challenge icon

Challenge

Medium

A food truck saved a week of orders in truck.csv: day, dish, payment and total (dollars). The starter reads the column to group by into column.

  1. Print the number of orders in each group.
  2. Print one table of each group's total, with the columns min, mean and max, in that order.

For the input day:

day
Fri    5
Mon    3
Thu    5
Tue    7
Wed    4
dtype: int64
     min   mean  max
day                 
Fri   10  19.60   36
Mon   12  27.00   42
Thu   10  21.60   42
Tue    9  19.00   30
Wed   10  21.25   27

Try it yourself

import pandas as pd

orders = pd.read_csv("truck.csv")
column = input()

# Write your code below
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Data and Plots

Practice on your own: Online Python compiler