I have a DataFrame with the columns below:
A B C D
ID
276 4 7.0 30 0
76 2 3.0 15 0
203 7 9.5 15 11
197 7 9.5 11 0
261 8 8.5 9 11
119 2 8.0 15 12
547 7 2.5 10 0
385 13 10.0 30 0
202 6 9.5 11 0
286 4 9.0 30 0
459 4 9.5 7 0
36 2 3.0 15 0
442 5 9.0 7 0
554 6 8.0 6 0
309 2 7.5 30 0
334 5 7.0 15 0
238 2 8.5 18 0
6 2 6.0 30 0
237 2 7.5 24 0
509 3 7.0 30 0
319 3 6.0 18 0
179 6 9.0 18 0
328 4 8.0 18 0
I'm attempting to split this DataFrame into multiples by column 'A' where the sum of the values is less than or equal to w with no defined limit on the number of splits or IDs
(i.e. df['A'].sum() <= w)
And if possible, group by the largest values of column 'B' first.
The resulting DataFrames should look like this:
A B C D
ID
385 13 10 30 0
203 7 9.5 15 11
A B C D
ID
197 7 9.5 11 0
202 6 9.5 11 0
459 4 9.5 7 0
A B C D
ID
286 4 9 30 0
442 5 9 7 0
179 6 9 18 0
A B C D
ID
261 8 8.5 9 11
238 2 8.5 18 0
119 2 8 15 12
554 6 8 6 0
A B C D
ID
328 4 8 18 0
309 2 7.5 30 0
237 2 7.5 24 0
276 4 7 30 0
334 5 7 15 0
509 3 7 30 0
A B C D
ID
6 2 6 30 0
319 3 6 18 0
76 2 3 15 0
36 2 3 15 0
547 7 2.5 10 0
What is the simplest method to do this?