Understand the idea
A DataFrame built from Series matches their index labels, regardless of displayed order.
Illustrative data
pd.DataFrame({"mean": means, "records": counts})| Label | Mean |
|---|---|
| A | 4 |
| B | 9 |
| Label | Records |
|---|---|
| B | 3 |
| A | 2 |
| Label | mean | records |
|---|---|---|
| A | 4 | 2 |
| B | 9 | 3 |
Follow the code
pd.DataFrame(
{
"mean": means,
"records": counts,
}
)What the syntax does
pd.DataFrame({"mean": means, "records": counts})- Match the Series by bench label.
series.index- Labels used to match values.
series.to_numpy()- Remove labels; table construction then matches positions.
bench | reading |
|---|---|
| Ash | 9 |
| Cedar | 6 |
| Birch | 2 |
| Ash | 10 |
| Cedar | 8 |
| Birch | 4 |
| Birch | 60 |
| Ash | 11 |
Your task · Follow
How can summaries in different orders be combined safely?
- Use supplied Series
meansandcountsfromdf. - Display a
DataFramewithmean, thenrecords, aligned bybenchlabel. - Keep
benchlabels as the index.
Setup is included in the editor.
Hint
Pass the Series themselves to the constructor so pandas can match labels.
Reveal solution
Keep the supplied setup and replace the Your work section with this example.
pd.DataFrame(
{
"mean": means,
"records": counts,
}
)