Pandasdrill

Level 6: From Excel and SQL Exercise 80 of 102

Excel XLOOKUP: bring a price into each row

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

In Excel you would add a column with `XLOOKUP(sku, pricelist)`. Do the same in pandas: return `lines` with one extra column, `price`, looked up from `pricelist` by `sku`. Keep every row of `lines` in its original order. A product that is not in the price list gets a missing price.

What this exercise teaches

A left merge keeps every row of lines and adds the matching price. Rows without a match get NaN, the pandas version of #N/A. An inner merge, the default, would silently drop the product that has no price.

A hint if you get stuck

A lookup is a merge that keeps every row of the left table. Which value of how= does that?

Try it now, free, no account needed

Your first 8 exercises 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 unlocks every exercise, timed mode and the certificate.

Open this exercise

More exercises in From Excel and SQL