Pandasdrill

Level 2: Cleaning Exercise 29 of 102

Limit values to a range with clip

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 `stock`, a negative `qty` is a data entry error and the warehouse never holds more than 100 of an item. Return the `qty` column with every value below 0 raised to 0 and every value above 100 lowered to 100. Keep all rows.

What this exercise teaches

clip caps a column at both ends in one step and keeps every row, which is what you want for outliers you would rather tame than delete. Filtering with between would drop those rows instead.

A hint if you get stuck

clip(lower=..., upper=...) moves values outside the range to the nearest limit and leaves the others alone.

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 Cleaning