Understand the idea
A grouped Series holds categories in its index and their means in its values.
Illustrative data
Click the highlighted code to change the result.
means = df.groupby()["reading"].mean()
list(means.index)| row | bench | station | reading |
|---|---|---|---|
| 0 | A | North | 2 |
| 1 | A | South | 4 |
| 2 | B | North | 8 |
| 3 | C | South | 6 |
bench | reading |
|---|---|
| A | 3 |
| B | 8 |
| C | 6 |
Result
Grouping creates one mean per label. means.index returns those group labels, rather than the mean measurements.
Follow the code
medians = df.groupby("bench")["reading"].median()
ranked = medians.sort_values()
order = ranked.index
list(order)What the syntax does
means = df.groupby("bench")["reading"].mean()- Calculate one mean per bench; bench names become the Series index.
list(means.index)- Return those bench labels in their existing order.
bench | reading |
|---|---|
| Ash | 9 |
| Cedar | 6 |
| Birch | 2 |
| Ash | 10 |
| Cedar | 8 |
| Birch | 4 |
| Birch | 60 |
| Ash | 11 |
Your task · FollowWhich category labels belong to the grouped
Which category labels belong to the grouped means?
- Group all
dfrows bybenchand calculate meanreading. - Display the grouped category labels as a list in default label order.
- Use the grouped Series
index.
Setup is included in the editor.
Hint
Group names become the summary index; the means are different values.
Reveal solution
Keep the supplied setup and replace the Your work section with this example.
means = df.groupby("bench")["reading"].mean()
list(means.index)