ClSt
ClSt

Reputation: 3

Pandas: How to add up values, only if they have the same number

I have a list of orders. Every order has a order-number (1, 2, 3, 4...). If a customer ordered more then 1 Item, the order-number will be the same.

enter image description here

Now I'd like to sumarize the prices oft the Items in a new column if the order number is the same. Is there a way to do that?

Thank you very much for your help!

Upvotes: 0

Views: 1352

Answers (1)

Ynjxsjmh
Ynjxsjmh

Reputation: 30032

You can use transform('sum') on grouped by, which will return a Series with the same index as the original dataframe. Sum value of each group is assigned to each group index.

df['sum'] = df.groupby('Order')['Price'].transform('sum')

Upvotes: 2

Related Questions