0

I have this dataframe:

a = ["A", "A", "B", "B", "A", "A"]
b = [12, 15, 14, 17, 19, 20]
c = ["Area1", "Area1", "Area2", "Area2", "Area3", "Area3"]

df = pd.DataFrame({'Item': a, 'Quanity': b, 'Region': c})

I want to get the sum for each Item and for each Region, the result should be like this:

Item  Region  total
A     Area1   27
B     Area2   31
A     Area3   39  

I have tried this method:

df['total'] = df.groupby(['Item', 'Region'])['Quanity'].transform('sum')
df[["Item", "Region", "total"]].drop_duplicates(keep='first')

However this method causes me to create another column and another dataframe (maybe). So are there any way faster (or more efficient) to do this task?

Long_NgV
  • 453
  • 1
  • 3
  • 16

0 Answers0