Search code examples
pythonarraysbinning

Re-binning python array


Let's say I have arrays

x=[0,0.5,1.7,4,5.5,5.7,8,10]
y=[10,23,2,3,55,67,74,20]

When plotted, it will show values 0 to 10 on the x-axis, however, the data is not binned to integer values on x. Is there a way we can bin values to x=[0,1,2,3....10]. I know interpolation can help us bin the values, any other method?


Solution

  • Numpy can help.

    import numpy as np
    x=[0,0.5,1.7,4,5.5,5.7,8,10]
    y=[10,23,2,3,55,67,74,20]
    
    z = np.interp([0,1,2,3,4,5,6,7,8,9,10], x, y)
    

    z = array([10,14.25,2.13043478,2.56521739,3,37.66666667,14.86956522,44.43478261,74,47,20])

    Hope this helps.