Add age_months to df from age in years (12 months per year).
The editable setup on the right creates df. Run executes the setup and your work from top to bottom.
Your requirements
- Keep the original age values and all other columns; display df.
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
Arithmetic on columns works row by row. Assigning to a new column name stores a derived measurement alongside the originals.
A small example
Prices [3, 5] plus tips [1, 2] give totals [4, 7]. Ages [2, 3] years become [24, 36] months.
| Code or choice | Meaning |
|---|---|
df["new"] = column * number | Apply the same multiplier to every value. |
df["new"] = first + second | Combine matching row values; use compatible units. |
Hint
A unit conversion belongs in a new, clearly named column.
Reveal solution
One way to do it. Keep any supplied setup in the editor and use this in the Your work section.
df["age_months"] = df["age"] * 12
df