Reputation: 8247
I have following pandas dataframe
ID Quantity Key Product Code decant
0 10 12 HS MT 123
1 13 13 HS LT 124
2 15 13 HS LT 124
3 10 14 MS PQ 145
4 50 15 MS PQ 146
My desired dataframe would be
ID Quantity Key Product Code decant
0 10 12 HS MT 123
1 28 13 HS LT 124
2 10 14 MS PQ 145
3 50 15 MS PQ 146
I want add Quantity
where Key
is duplicated. I know we can do a groupby and add. But is there any simpler way to do it with numpy
? Because I have other columns as well,so groupby will not be a suitable solution
Upvotes: 0
Views: 240
Reputation: 71570
Try groupby
:
print(df.groupby('Key',as_index=False).sum())
if want to fix ID
:
df2=df.groupby('Key',as_index=False).sum()
df2['ID']=df2.index
print(df2)
Update try:
print(df.groupby(['Key','Product','Code'],as_index=False).sum())
If want to fix ID
:
df2=df.groupby(['Key','Product','Code'],as_index=False).sum()
df2['ID']=df2.index
print(df2)
Upvotes: 2