I have the following data in pandas df:
quantity color
1 White
3 White
1 Black
2 Black
I need to group duplicates and get the sum of their quantities:
quantity color
4 White
3 Black
I have tried the following code to first sort by quantity, then using groupby to combine duplicates and get the sum.
df1 = df.sort_values(by=['quantity','color'], ascending=False)
df2 = df1.groupby(['color']).sum()
print(df2)
I'm getting the following output:
White 13
Black 12
As you can see it's not giving me the sum of the quantity, it's just listing those values side by side in the next column (1,3) and (1,2) for each color.