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 exerciseMore exercises in Cleaning
- Fill missing values in one column
- Drop rows missing a key field
- Convert text to numbers
- Tidy up text with strip and lower
- Search text with str.contains
- Pull numbers out of codes
- Remove duplicate records
- Rename columns and replace a placeholder
- Dates with to_datetime and dt
- Clean messy money text
- Split a column into two
- Fill gaps with interpolate
- Parse dates in day/month/year order
- Text to numbers, bad values to NaN
- Recode values with map