I have a Pandas dataframe with the following columns
date | months | price
I calculate some basic BI metrics. I did the Net Revenue by grouping the dataframe on date and sum the price:
df = df[["Date", "Price"]].groupby(df['Date'])["Price"].sum().reset_index()
Now, I want to find the MRR, which is similar to the Net Revenue, but in case the column months have more than 1 month, the price should be "moved" equally to the next months. And also, it is grouped by month and not day.
For example, if I am on January 2016 and I have a row with 3 months and price 30$, I should add 10$ to January, 10$ to February and 10$ to March.
My first idea was to iterate through the dataframe, keep track of the months and the amount of price I should "move" on next months and create a new dataframe manually.
But, first, is there any Pythonic way in Pandas to do it?
Data to reproduce a dataframe:
import pandas as pd
df = pd.DataFrame({'date': ['01-01-2016', '05-01-2016', '10-01-2016','04-02-2016'],
'months': [1, 3, 1, 6],
'price': [40, 60, 20, 60]})
Desired result:
Date | MRR
January 2016 | 80
February 2016| 30
March 2016 | 10
April 2016 | 10
May 2016 | 10
June 2016 | 10
July 2016 | 10
And the results calculated like this for each row
January 2016 = 40 + 20 + 20 + 0
February 2016 = 0 + 20 + 0 + 10
March 2016 = 0 + 0 + 0 + 10
April 2016 = 0 + 0 + 0 + 10
May 2016 = 0 + 0 + 0 + 10
June 2016 = 0 + 0 + 0 + 10
July 2016 = 0 + 0 + 0 + 10