Using pandas.cut() in Python for Data Analysis: Creating Number and Date Intervals
Tags: python, datascience, analytics, programming
Published: 2023-05-13 12:22:30

In this article, we will explore how to use the pandas.cut() method to create number and date intervals for data analysis.

## What is pandas.cut()?

Python _pandas.cut()_ is a method in the pandas library that allows you to split a continuous variable into intervals.

This method creates a new categorical variable based on the bins you specify.

The bins can be specified as a list of numbers or as a number of evenly spaced intervals.

This method is commonly used in data analysis to group continuous data into categories or bins. This is useful to create categories for data transformation, time series analysis and to turn data visualizations more informative.

If you want a deeper understanding about those subjects, I recommend the book **[Python for Data Analysis](https://amzn.to/41AM571)**, a definitive guide on how to deal with data using Python. You can find it **[here](https://amzn.to/41AM571)**.

Now, let’s move to the first example on how to use _pandas.cut()_ method.

## Creating Number Intervals with pandas.cut()

Suppose we have a dataset of student grades, and we want to categorize them into letter grades (A, B, C, D, and F).

We can do this by creating bins based on the grade ranges.


![Screenshot of code importing pandas library and creating a series called grades](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s3u38p7ph435ovtvlada.png)

Now, let’s create the bins for the grades:

![Series called bins](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/40fwbzncvh1wef2lsuwb.png)

We want to categorize the grades into the following letter grades: F (below 60), D (60–69), C (70–79), B (80–89), and A (90–100).

We can achieve this by using the _pandas.cut()_ method:

![letter_grades variable created using pandas.cut()](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/67plni1z3up6jcsxhpas.png)

The resulting variable _letter_grades_ is a categorical variable with the letter grades for each grade in the dataset.

![letter_grades variable](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v1dvi97smg7aspbp2elp.png)

You can also sort and group it, if you would like:

![letter_grades variable grouped and sorted](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gpg2fut32fwlpz0rftbo.png)

## Creating Date Intervals with pandas.cut()

Now let’s see how to use pandas.cut() to create date intervals.

Suppose we have a dataset of daily sales, and we want to categorize them into monthly intervals. We can do this by creating bins based on the month ranges.

First, once more, we need to import the pandas library and create a sample dataset:

![Import pandas library and create a dataset](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2r9sruv02hah0bixjyr6.png)

Now, let’s create the bins for the sales:

![Create bins for monthly intervals](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0x0inj0ge6h880fj4xon.png)

And the labels:

![Create labels for each interval](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3z2eg253jumej201t0p1.png)

We want to categorize the sales into monthly intervals. We can achieve this by using the _pandas.cut()_ method:

![Categorize sales data into monthly intervals using pandas.cut() method](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jws4gwoe5yrxr6pb9xo4.png)

And that’s the result:

![Print the resulting data frame](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hhabumra3ql6x55vjux9.png)

Grouping numbers in intervals is useful to plot concise charts, in this case, using monthly_sales in the X-axis. It turns the chart more compact and easier to read.

This is crucial when presenting data, as explained in **[Storytelling with Data](https://amzn.to/3HaAV0D)**, the definitive handbook on how to communicate effectively with data.

Find it **[here](https://amzn.to/3HaAV0D)**.

## Conclusion

In conclusion, _pandas.cut()_ is a powerful method in the pandas library that allows you to split a continuous variable into intervals.

By using this method, you can create categorical variables for data analysis and draw insights from raw data.

If you want to learn more about data analysis with Python, I highly recommend the following books:

- [Python for Data Analysis 3e: Data Wrangling with pandas, NumPy, and Jupyter](https://amzn.to/3H9EDrm)
- [Think Python: How to Think Like a Computer Scientist](https://amzn.to/40I9dQ4)
- [Learning Python: Powerful Object-Oriented Programming](https://amzn.to/3oEzL7m)
- [Time Series Forecasting in Python](https://amzn.to/41Oyk4q)