How to Perform Data Binning in Python

Hello programmers, in this tutorial, we will learn how to Perform Data Binning in Python.

Data Binning: It is a process of converting continuous values into categorical values.

Let’s start coding:

  • 1st we will create a random number array of the age of continuous values.
  • Then we will create a DataFrame using pandas and store all that random age in that DataFrame
#importing random and pandas
import random
import pandas as pd
#creating 30 random values between 10 to 70
age =random.sample(range(10, 70),30)
#creating DataFrame


  • Then we will split it into three categories of young, senior, most-senior
  • For this, we create a bin and a labeled list
  • At last, we use the cut() method to split our data into categorical values
bins = [10, 20, 40, 70] 
group_names= list(['young','Senior','Senior-most'])
bined_age = pd.cut(df["age"], bins, labels=group_names)



Hopefully, you have learned how to Perform Data Binning in Python.

Leave a Reply

Your email address will not be published. Required fields are marked *