Quartile bins with qcut
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
Split customers into four groups with equal numbers of customers by `spend`, labeled "Q1" (lowest) to "Q4" (highest). Return the labels as a Series.
What this exercise teaches
With skewed spending, equal-width bins from cut would put almost everyone in the first bin. qcut uses the 25th, 50th and 75th percentiles as edges, so each label covers a quarter of the customers.
A hint if you get stuck
qcut cuts at quantiles, so each bin holds about the same number of rows. cut splits the range into equal widths instead.
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.