Understand 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.
Follow the code
Apply the idea to the supplied table. Read from top to bottom; the final line displays the result.
df["pair_price"] = df["price"] * 2
dfWhat each part does
df["pair_price"] =- Create a new column called pair_price on the left.
df["price"] * 2- Multiply each existing price by two on the right, row by row.
Your inputs
The editable setup on the right creates df. Run executes the setup and your work from top to bottom.
| candy | flavour | price | rating | shelf |
|---|---|---|---|---|
| Gummy Bear | fruity | 1.2 | 4.1 | A |
| Choco Pop | chocolate | 2.1 | 4.6 | B |
| Mint Bite | mint | 1.5 | 3.8 | A |
| Berry Loop | fruity | 2.8 | 4.4 | B |
| Cocoa Cube | chocolate | 3.4 | 4.9 | A |
| Lemon Drop | fruity | 1.8 | 4 | B |
Your task · Follow
- The shop offers a two-item pack of each candy.
- Using df unit prices, add pair_price equal to twice price.
- Keep the original columns and display df.
Hint
A two-item pack costs two unit prices under this offer.
Reveal solution
One way to do it. Keep any supplied setup in the editor and use this in the Your work section.
df["pair_price"] = df["price"] * 2
df