Compare two price lists with suffixes
A pandas exercise from Pandasdrill. Solve it with a Python editor in your browser running real pandas, and it is checked the moment you submit.
The task
`old` and `new` are price lists. Return the products present in both whose price changed, as a DataFrame with the columns `product`, `price_old` and `price_new`. Use merge suffixes to name the two price columns. Row labels do not matter.
What this exercise teaches
An inner merge on product lines up the two prices for products in both lists, and suffixes=("_old", "_new") gives them readable names instead of the default _x and _y. An outer merge would include products that were added or dropped, and since NaN != 9.0 is True they would be reported as changed.
A hint if you get stuck
When both tables have a column with the same name, merge adds suffixes to tell them apart. The suffixes parameter sets them.
Try it now, free, no account needed
The first 15 tasks on Pandasdrill are free. Open it on the practice page, solve it, and get instant feedback with a hint. A free account adds the worked solution; Pro adds timed mode and the certificate.
Open this exerciseMore exercises in Combining and reshaping
- Inner merge on a shared keyPro
- Left merge keeps every orderPro
- Merge on differently named keysPro
- Stack two tables with concatPro
- Merge twice, then aggregatePro
- Wide to long with meltPro
- Long to wide with pivotPro
- Columns into rows with stackPro
- Index level into columns with unstackPro
- Orders with no customer, via indicatorPro
- Label the source with concat keysPro
- One row per tag with explodePro