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:
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: int64Comedies 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
MediumA 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.
- Print the number of orders in each group.
- Print one table of each group's
total, with the columnsmin,meanandmax, 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 27Try it yourself
import pandas as pd
orders = pd.read_csv("truck.csv")
column = input()
# Write your code below
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Data and Plots
2NumPy Arrays
Creating ArraysShape and SizeRanges of NumbersIndexing and SlicingRecap: Temperature Grid3Array Math
Math on Whole ArraysSummary StatisticsStatistics by AxisBroadcastingBoolean MasksRecap: Exam Scores9Grouping and Cleaning
Counting ValuesGrouping DataSeveral SummariesMissing ValuesRecap: Survey ResultsPractice on your own: Online Python compiler