Skip to content
Advertisement

Getting column mean by summing values of another column

I have a Dataframe with three columns Customer, Purchase amount and Purchase date.

I need to get sum of Purchase amount of customer purchases for each day, then get mean of purchases for each customer for one month.

I’ve tried to use df.pivot_table() but it takes mean of purchases for one day and then mean of purchases for one month.

Hope you can help me.

My Dataframe:

JavaScript

I’ve tried to use:

JavaScript

Result:

JavaScript

Here is what I need to get:

JavaScript

Advertisement

Answer

JavaScript

For getting the sum for each day use groupby

JavaScript

output

JavaScript

For monthly average again use groupby

JavaScript

output

JavaScript

Looking at what you are trying to achieve can be done using pivot table as

JavaScript

output

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement