Member-only story
Pandas — Split Data Into Buckets With cut And qcut
If you do a lot of data analysis on your daily job, you may have encountered problems that you would want to split data into buckets or groups based on certain criteria and then analyse your data within each group. For instance, you would like to check the popularity of your products or website within each age groups, or understand how many percent of the students fall under each score range. The most straightforward way might be to categorize your data based on the conditions and then summarize the information, but this usually requires some additional effort to massage the data. In this article, I will be sharing with you a simple way to bin your data with pandas cut and qcut function.
Prerequisite
You will need to install pandas package if you do not have it yet in your working environment. Below is the command to install pandas with pip:
And let’s import the necessary packages and create some sample sales data for our later examples.
import pandas as pd
import numpy as np df = pd.DataFrame({
"Consignee" : ["Patrick", "Sara", "Randy", "John", "Patrick", "Joe"],
"Age" : [44, 51, 23, 30, 44, 39],
"Order Date" : pd.date_range(start='2020-08-01', end="2020-08-05", periods=6),
"Item Desc" : ["White Wine", "Whisky", "Red Wine", "Whisky", "Red…