Combine supplied first and second, then keep records with age above 2.
The editable setup on the right creates df and the additional inputs shown below. Run executes the setup and your work from top to bottom.
Your requirements
- Display them sorted by age, largest first, with a fresh consecutive index.
Your inputs
| name | species | age | weight | room |
|---|---|---|---|---|
| Milo | Cat | 3 | 4.2 | A |
| Pepper | Dog | 7 | 18.5 | B |
| Luna | Cat | 2 | 3.6 | A |
| Bean | Rabbit | 4 | 2.4 | B |
| Rex | Dog | 5 | 22 | A |
| Nori | Rabbit | 1 | 1.8 | B |
| name | species | age | weight | room |
|---|---|---|---|---|
| Milo | Cat | 3 | 4.2 | A |
| Pepper | Dog | 7 | 18.5 | B |
| Luna | Cat | 2 | 3.6 | A |
| name | species | age | weight | room |
|---|---|---|---|---|
| Bean | Rabbit | 4 | 2.4 | B |
| Rex | Dog | 5 | 22 | A |
| Nori | Rabbit | 1 | 1.8 | B |
Remember the idea
concat appends tables in the order listed. It matches column names, rather than looking for matching records.
A small example
Batches with indices [0, 1] and [0, 1] produce [0, 1, 0, 1], or [0, 1, 2, 3] with ignore_index=True.
| Code or choice | Meaning |
|---|---|
pd.concat([first, second]) | Append second below first and retain existing row labels. |
ignore_index=True | Number the combined rows consecutively from zero. |
Hint
Finish filtering and sorting before assigning presentation indices.
Reveal solution
One way to do it. Keep any supplied setup in the editor and use this in the Your work section.
combined = pd.concat([first, second], ignore_index=True)
combined[combined["age"] > 2].sort_values("age", ascending=False).reset_index(drop=True)