Pandasdrill

Level 6: From Excel and SQL Exercise 83 of 102

Excel IFS: three size bands

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

Excel would use `IFS`. Add a column `size` that is `small` for amounts under 150, `medium` for 150 up to but not including 300, and `large` for 300 and above. Return the columns `order_id`, `amount` and `size` for every order.

What this exercise teaches

np.select is IFS for a whole column: the first condition that is True wins, so the second test only needs an upper bound. Watch the edges. An amount of exactly 150 is medium and exactly 300 is large, which is why the tests use < and not <=.

A hint if you get stuck

np.select takes a list of conditions and a list of results, checks the conditions in order and uses default when none is True.

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