Pandasdrill

Level 5: Analysis Exercise 45 of 45 Pro

Classify with apply

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

Write a function that labels an order "small" when qty is under 5, "medium" from 5 to 19, and "large" at 20 or more. Apply it to the `qty` column and return the labels as a Series.

What this exercise teaches

Series.apply calls your function once per value and collects the results in a new Series with the same index. Watch the boundaries: 5 is medium and 20 is large.

A hint if you get stuck

Define a plain Python function that takes one number and returns a string, then pass the function itself (no parentheses) to apply.

This exercise is part of Pro

Pro unlocks all 45 exercises, with worked solutions and progress saved to your account. The free exercises work without an account.

You create a free account first, so Pro is attached to it.

Monthly is charged today and then every month until you cancel, in one click from your account page. Lifetime is one payment with no renewals. Full refund within 14 days of paying.

Start with the free exercises

More exercises in Analysis