Home > other >  How can I sum 2 columns with Pandas to get sales and quantity values for a third column?
How can I sum 2 columns with Pandas to get sales and quantity values for a third column?

Time:08-25

I have a dataframe that contains sales info.

Animal Quantity Sold Total Sales
Panda 3 15
Fox 1 5
Bear 2 10
Panda 1 5
Fox 1 5

I want to get the total quanity and sales for each animal so the end output i would want is:

Animal Quantity_Sold Total_Sales
Panda 4 20
Bear 2 10
Fox 2 10

How can I achieve this? I can get the sum of either quantity or sales and aggregate the animal by using the following:

df2 = df[['Animal', 'Quantity_Sold', 'Total_Sales']]
df2 = df2.groupby('Animal')['Total_Sales'].agg('sum')
df2

How do I sum both and have the animals aggregate? Any help appreciated :)

CodePudding user response:

Just don't slice:

df2.groupby('Animal', as_index=False).sum()

Or directly from df, slice the columns to aggregate:

df.groupby('Animal', as_index=False)[['Quantity Sold', 'Total Sales']].sum()

output:

  Animal  Quantity Sold  Total Sales
0   Bear              2           10
1    Fox              2           10
2  Panda              4           20

CodePudding user response:

You're very close. Just select both columns before you aggregate.

df2 = df[['Animal', 'Quantity_Sold', 'Total_Sales']]
df2 = df2.groupby('Animal')[["Quantity_Sold", 'Total_Sales']].agg('sum')

Output:

        Quantity_Sold  Total_Sales
Animal                            
Bear                2           10
Fox                 2           10
Panda               4           20
  • Related