0

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

0

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

4 Comments

I have edited the question. I have other columns as well which are of type object.
@Neil Edited mine
I have 9 more numeric columns,in that case what would be the solution? Edited the question just for a reference
you can use groupby().agg() to aggregate different columns: pandas.pydata.org/pandas-docs/stable/generated/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.