0

I have data frame like this

enter image description here

I need to bin 'a' column into n intervals (5 for example), but intervals must have equal or similar sum of 'b' value

I want result similar to pd.cut on 'a' column

enter image description here

But if we group by sum must be equal or similar (not as on next pic, made with pd.cut) enter image description here

chikich
  • 40
  • 4
  • I updated what I want as a result – chikich Sep 29 '22 at 14:12
  • That's a pretty complex situation. I'd say, for this particular example, `df.groupby(pd.cut(df.a, [0,1,3,5,8,10])).b.sum()` would get you fairly close (i.e. `[20, 30, 30, 30, 30]`), but I'm just doing that manually. Not sure what the calculation for that answer would look like, maybe this [answer](https://stackoverflow.com/a/42271733/18470692) could help in steering you in the right direction. – ouroboros1 Sep 29 '22 at 14:44

0 Answers0