I want to group by the zip code and form tucks, but if it hits 30000 it should form another truck. I am not able to apply group by and rank it. It might be required to sort the weights in the ascending order to form the right truck. Any help would be really appreciated.
I have the following data:
Load No. Zip Code Pounds
1 50507 20000
2 50507 8000
3 50507 5000
4 60001 28000
5 60001 30000
6 60001 2000
7 60001 4000
8 60002 20000
9 60002 18000
10 60002 13000
Output:
Load No. Zip Code Pounds Truck Total Weight
1 50507 20000 1 28000
2 50507 8000 1 28000
3 50507 5000 2 5000
4 60001 28000 3 30000
5 60001 30000 5 2000
6 60001 2000 3 30000
7 60001 4000 4 4000
8 60002 20000 6 20000
9 60002 18000 7 18000
10 60002 13000 8 13000
I have sorted the data frame: data=data.sort_values(by=['Zip Code','Pounds'])
Also tried grouping by Zip Code but failing to put in the condition(>20000) to form a dense rank: data['Total weight'] = data.groupby('Zip Code')['Pounds'].transform(sum)