You can use binned_statistic from scipy.stats , which supports various statistical functions that will be applied in chunks in a 1D array. To get the pieces, we need to sort and get the position of the shifts (where the pieces are changing), for which np.unique would be useful. Introducing all this, here's the implementation -
from scipy.stats import binned_statistic as bstat
In binned_statistic documents, binned_statistic can also use a custom binned_statistic function:
function: a user-defined function that takes a 1D array of values and displays a single numerical statistic. This function will be called by the values in each hopper. Empty baskets will be represented by function ([]) or NaN if this returns an error.
Example input, output -
In [121]: data Out[121]: array([[2, 5], [2, 2], [1, 5], [3, 8], [0, 8], [6, 7], [8, 1], [2, 5], [6, 8], [1, 8]]) In [122]: np.column_stack((unq_x,avg_y,std_y)) Out[122]: array([[ 0. , 8. , 0. ], [ 1. , 6.5 , 1.5 ], [ 2. , 4. , 1.41421356], [ 3. , 8. , 0. ], [ 6. , 7.5 , 0.5 ], [ 8. , 1. , 0. ]])