It's probably faster and easier to use numpy.digitize():

import numpy
data = numpy.random.random(100)
bins = numpy.linspace(0, 1, 10)
digitized = numpy.digitize(data, bins)
bin_means = [data[digitized == i].mean() for i in range(1, len(bins))]

An alternative to this is to use numpy.histogram():

bin_means = (numpy.histogram(data, bins, weights=data)[0] /
             numpy.histogram(data, bins)[0])

Try for yourself which one is faster... :)

Answer from Sven Marnach on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › numpy › binning-data-in-python-with-scipy-numpy
Binning Data In Python With Scipy & Numpy - GeeksforGeeks
July 23, 2025 - Binning data is a common technique in data analysis where you group continuous data into discrete intervals, or bins, to gain insights into the distribution or trends within the data. In Python, the numpy and scipy libraries provide convenient functions for binning data...
People also ask

What are the different techniques for binning data in Python?
The most common techniques for binning data in Python include equal-width binning, equal-frequency binning, and k-means clustering. Python libraries like NumPy and Pandas provide functions to implement these techniques.
🌐
docs.kanaries.net
docs.kanaries.net › topics › Python › python-binning
Python Binning: Clearly Explained – Kanaries
What is Python binning?
Python binning is a data preprocessing technique used to group a set of continuous values into a smaller number of "bins". It can help improve accuracy in predictive models, especially when dealing with overfitting.
🌐
docs.kanaries.net
docs.kanaries.net › topics › Python › python-binning
Python Binning: Clearly Explained – Kanaries
What are the benefits of binning in Python?
Binning in Python can help reduce noise, transform continuous variables into categorical counterparts, and improve the performance of machine learning models.
🌐
docs.kanaries.net
docs.kanaries.net › topics › Python › python-binning
Python Binning: Clearly Explained – Kanaries
🌐
Python Course
python-course.eu › numerical-programming › binning-in-python-and-pandas.php
34. Binning in Python and Pandas | Numerical Programming
February 3, 2025 - The module Pandas of Python provides powerful functionalities for the binning of data.
🌐
Statology
statology.org › home › how to perform data binning in python (with examples)
How to Perform Data Binning in Python (With Examples)
December 14, 2021 - The following code shows how to perform data binning on the points variable using the qcut() function with specific break marks:
🌐
Train in Data
blog.trainindata.com › master-data-binning-in-python-using-pandas
Master Data Binning in Python using Pandas | Train in Data Blog
February 23, 2023 - In this tutorial, we’ll look into binning data in Python using the cut and qcut functions from the open-source library pandas.
🌐
CodeSignal
codesignal.com › learn › courses › data-cleaning-and-preprocessing-techniques › lessons › data-binning-techniques-an-introduction-and-implementation-with-python-and-pandas
An Introduction and Implementation with Python and Pandas
In the example above, we utilized the pd.cut() function to divide a set of ages into distinct age groups or bins. This approach allows us to categorize a wide range of ages into a selected age group, simplifying data analysis.
Find elsewhere
🌐
SciPy
docs.scipy.org › doc › scipy › reference › generated › scipy.stats.binned_statistic.html
binned_statistic — SciPy v1.18.0 Manual
Scientific Python Forum · Search Ctrl+K · scipy.stats. scipy.stats.binned_statistic(x, values, statistic='mean', bins=10, range=None)[source]# Compute a binned statistic for one or more sets of data. This is a generalization of a histogram function. A histogram divides the space into bins, and returns the count of the number of points in each bin.
🌐
Practical Business Python
pbpython.com › pandas-qcut-cut.html
Binning Data with Pandas qcut and cut - Practical Business Python
When dealing with continuous numeric data, it is often helpful to bin the data into multiple buckets for further analysis. There are several different terms for binning including bucketing, discrete binning, discretization or quantization. Pandas supports these approaches using the cut and qcut functions.
🌐
Kanaries
docs.kanaries.net › topics › Python › python-binning
Python Binning: Clearly Explained – Kanaries
August 17, 2023 - PyGWalker (opens in a new tab) ... User Interface for visual exploration. ... Python binning is a data preprocessing technique used to group a set of continuous values into a smaller number of "bins"....
🌐
IncludeHelp
includehelp.com › python › how-to-perform-data-binning-in-python.aspx
How to perform data binning in Python?
November 21, 2023 - For this Python has added many libraries with methods to perform such tasks with efficiency. For performing data binning in Python, use the qcut() method present in the pandas library.
🌐
YouTube
youtube.com › make data useful
Data Binning Explained: A Python and Pandas Tutorial - YouTube
Learn how to bin/group data using pure Python and the Pandas cut method. Thanks for the great question Matt! In this video we continue our CSV import and use...
Published: August 22, 2021
Views: 7K
🌐
Statology
statology.org › home › how to bin variables in python using numpy.digitize()
How to Bin Variables in Python Using numpy.digitize()
May 24, 2022 - Often you may be interested in placing the values of a variable into “bins” in Python. Fortunately this is easy to do using the numpy.digitize() function, which uses the following syntax: ... right: Indicating whether the intervals include the right or the left bin edge. Default is that the interval does not include the right edge. This tutorial shows several examples of how to use this function in practice. The following code shows how to place the values of an array into two bins: ... import numpy as np #create data data = [2, 4, 4, 7, 12, 14, 19, 20, 24, 31, 34] #place values into bins np.digitize(data, bins=[20]) array([0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1])
🌐
Arab Psychology
scales.arabpsychology.com › home › how to easily perform data binning in python with pandas.cut()
How To Easily Perform Data Binning In Python With Pandas.cut()
December 2, 2025 - The core objective of binning is simplification without significant information loss. By grouping nearby values into distinct categories, analysts can gain a clearer perspective on the underlying distribution and patterns within the data. In the Python data science environment, this essential task is managed primarily through the Pandas library, which provides specialized functions like pd.cut() and pd.qcut().
🌐
Towards Data Science
towardsdatascience.com › data-preprocessing-with-python-pandas-part-5-binning-c5bd5fd1b950
Data Preprocessing with Python Pandas — Part 5 Binning
May 9, 2022 - Your home for data science and AI. The world's leading publication for data science, data analytics, data engineering, machine learning, and artificial intelligence professionals.
🌐
Reddit
reddit.com › r/learnpython › binning data
r/learnpython on Reddit: Binning data
April 16, 2021 -

Hi all.

I'm an undergrad physicist and beginner-intermediate programmer and I'm looking to sort data for a recent experiment.

I have 2 variables; x and y.

I want to sort my large array of x and y values into small width bins defined by a small range of the x values. The x values run from 0 to 30 so lets say I want to to separate the data in around 90 bins which will give me a bin width of ~0.33.

So I want to have an array of bins that contain the small range of x and the corresponding y values in each bin.

For context, I'm trying to average 10 repeat runs of an experiment together and to calculate the standard deviation of each small width bin. So in essence each bin will have around 10-20 points in it and will produce a mean value which I can then use as a data point on the new, smoothed plot, with an error bar coming from the standard deviation.

Can anyone give me any hints on how I'd do this? Most of what I've seen treat it like a histogram, so I'd have a bin with some width, and a corresponding 'frequency' value telling me how many points are in this range, but I don't want this. For a range of x values within the width of the bin I want the corresponding y values.

I'm sorry if this is poorly explained guys! Google is usually my friend but I'm struggling to find what I need.

Thanks a lot <3

🌐
Towards Data Science
towardsdatascience.com › home › latest › how to bin numerical data with pandas
How to Bin Numerical Data with Pandas | Towards Data Science
January 28, 2025 - Bin values into discrete intervals. Use cut when you need to segment and sort data values into bins.
🌐
Medium
medium.com › @tubelwj › pandas-data-preprocessing-data-binning-40614ab13b54
Pandas Data Preprocessing — Data Binning | by Gen. Devin DL. | Medium
December 12, 2023 - ... Data binning is a type of data preprocessing, which is a mechanism for handling missing values (Pandas Data Preprocessing — Handling Missing Values), formatting (Pandas Data Preprocessing — Data Formatting), normalization (Pandas Data ...
🌐
Vultr Docs
docs.vultr.com › python › third party › pandas › cut()
Python Pandas cut() - Bin Values into Intervals
December 23, 2024 - The cut() function in Python's Pandas library serves as a utility to segment and sort data values into bins or intervals.
🌐
Llego
llego.dev › home › blog › histogramming and binning data with numpy in python
Histogramming and Binning Data with NumPy in Python - llego.dev
March 14, 2023 - Learn how to generate histograms and bin data in Python using NumPy's histogram(), digitize() and histogram2d() functions with code examples.
🌐
GitHub
github.com › alod83 › data-science › blob › master › Preprocessing › Binning › Data Preprocessing - Binning.ipynb
data-science/Preprocessing/Binning/Data Preprocessing - Binning.ipynb at master · alod83/data-science
"output_type": "display_data" } ], "source": [ "plt.hist(df['bin_cut_manual'], bins=3)\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Binning by frequency\n", "Binning by frequency calculates the size of each bin so that each bin contains the (almost) same number of observations, but the bin range will vary. We can use the Python `pandas` `qcut()` function.
Author: alod83