Create a compact preview of the last two rows of df: show name, then age.
The editable setup on the right creates df. Run executes the setup and your work from top to bottom.
Your requirements
- Display a DataFrame.
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
A list of column names keeps a DataFrame, even when the list has just one name. The list also sets the column order.
A small example
df[["age", "name"]] shows age first and name second, with every original row.
| Code or choice | Meaning |
|---|---|
df[["age"]] | Outer brackets select; inner brackets make a list. Result: one-column DataFrame. |
df[["age", "name"]] | Keep only these fields, in this order. |
df.tail(2)[["name", "age"]] | Combine a row preview with a column list. |
Hint
Combine a row preview with a deliberate column order.
Reveal solution
One way to do it. Keep any supplied setup in the editor and use this in the Your work section.
df.tail(2)[["name", "age"]]