Pandas .sum one column only (how to?)

Viewed 53

I have Data frame like this

Product     Q-ty    Price
Product A    5        10
Product B    1        3
Product B    2        3
Product A    1        10

I want to get something like this:

Product     Q-ty    Price
Product A    6        10
Product B    3        3

when I am trying this:

result = df.groupby(['Product']).sum()

I get this:

Product     Q-ty    Price
Product A    6        20
Product B    3        6

So it is making sum of both Q-ty and Price. I have triend:

result = df.groupby(['Product','Price'])['Q-ty'].sum()

but it didn't work... any idea how to do it?

1 Answers

You can group by using different aggregation funcions for each column:

df_output = df.groupby('Product').agg({'Q-ty':'sum',  'Price':'mean'})

Finally you can add df_output.reset_index() if you do not want to work with grouped dataframes.

Related