How to split values into discrete intervals categories with Pandas in Python

1 min

Using the pandas.cut() method you can split the distribution into discrete intervals.

import pandas as pd

# We read a sample dataset from the web.
df = pd.read_csv('https://raw.githubusercontent.com/'\
                 'mwaskom/seaborn-data/master/iris.csv')
 
 # We use the cut method to split into discrete intervals
 df["discrete_intervals"] = pd.cut(df["sepal_length"], 
 							 bins=10,
                             include_lowest=True)
                             
# We set the canvas
fig, axes = plt.subplots(1, 1, figsize=(6,4))

# We plot the graph
df.groupby(by="discrete_intervals")["discrete_intervals"].count()\
  .plot(kind='bar')

# We tidy the layout
fig.tight_layout()

# We save the fig
plt.savefig("discrete_intervals.png")
How to bin values into discrete intervals

Here is the result

Here you are! You now know how to split values into discrete interval categories.