Pandasdrill

Level 4: Combining and reshaping Exercise 50 of 63 Pro

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 exercise

More exercises in Combining and reshaping