Let's say that I have a data frame that has a column like this:
Weight
1
1
0.75
0.5
0.25
0.5
1
1
1
1
I want to create two bins and add a column to my data frame that shows which bin each row is in, but I don't want to bin on the observations (i.e. the first 5 observations got to bin 1 and the last five to bin 2). Instead, I want to bin such that the sum of weight for each bin is equal or as close to equal as possible without changing the order of the column.
So, I want the result to be
Weight I want Not this
1 1 1
1 1 1
0.75 1 1
0.5 1 1
0.25 1 1
0.5 1 2
1 2 2
1 2 2
1 2 2
1 2 2
Is there something built into Pandas that already does this, or can someone share any ideas on how to make this happen? Thanks!