Reputation: 39
I have a dataframe with 4 columns . The dataframe looks like this:
date sell price cost price discount
2019-10-13 2000 2000 0
2019-10-21 3000 3000 0
I need to find the total sum and average of 2 columns cost price and sell price. The output should be like:
total avg
sell price 5000 2500
cost price 5000 2500
How can I get this?
Upvotes: 0
Views: 1701
Reputation: 30920
Use DataFrame.agg
:
new_df=df[['sell_price', 'cost_price']].agg(['sum','mean']).T.rename(columns={'sum':'total','mean':'Avg'})
print(new_df)
total Avg
sell_price 5000.0 2500.0
cost_price 5000.0 2500.0
Upvotes: 1
Reputation: 1159
Use of aggregate should do this.
df.aggregate({"cost_price":['sum','mean'],"sell_price":['sum','mean']})
[update] why it does not work? Any error to see?
Upvotes: 0