Prepare a separate clean table for species="Cat", ordered by age largest first.
The editable setup on the right creates df. Run executes the setup and your work from top to bottom.
Your requirements
- Preserve df and display clean.
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 |
Remember the idea
Make a separate working table before editing. Assigning another name to df alone still refers to the same object.
A small example
clean = df.copy() lets you select or sort clean while keeping the original df available.
| Code or choice | Meaning |
|---|---|
clean = df | Another name for the same table; direct edits can affect df. |
clean = df.copy() | Independent working DataFrame for these scalar-valued tables. |
clean = clean[...] | Keep a selection in the working copy; display clean at the end. |
Hint
Combine copying, filtering and sorting without assigning back to df.
Reveal solution
One way to do it. Keep any supplied setup in the editor and use this in the Your work section.
clean = df.copy()
clean = clean[clean["species"] == "Cat"].sort_values("age", ascending=False)
clean