Skip to content
Advertisement

Grouping two variables based on third variable bins in python

My intension is to group two variables pairs falling within a third variable’s bin interval. such as: enter image description here

So that I can fetch a XY pair for a particular bin range of third variable, e.g., bin of 25-50 enter image description here

For the same, I tried to sort the “Z” variable and accordingly “X” and “Y” variables, with Z, Y, X = zip(*sorted(zip(Z,Y,X))), that works perfectly. Now, I have to bin the sorted “Z” variable in the interval of 25 such as bins=[0,25,50,75,100,125,150,200, rest all], here rest all means all >200 whatever available!

And in the last, I want to call the “X” and “Y” pairs falling in a particular bins of “Z”.

(plz find the datafile on https://drive.google.com/file/d/1qWM39NheJuEqgTycHhARwBcULmkX4k6B/view?usp=sharing)

I tried a lot, but didn’t work well. Any remarks from the community! Thanks

Advertisement

Answer

IIUC, you can try pd.cut

df['bins'] = pd.cut(df['Z'], bins=[0,25,50,75,100,125,150,200, np.inf])
res = df[df['bins'] == pd.Interval(25, 50)]
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement