I'm trying to break down yearly subscriptions to monthly subscriptions by fee.
Example dataset-
import numpy as np
import pandas as pd
df = pd.DataFrame({
'Customer_ID': [1, 2, 3, 4, 5],
'Plan' : ['Yearly', 'Monthly', 'Monthly', 'Yearly', 'Yearly'],
'Join_Date': ['1/10/2020', '1/15/2020', '2/21/2020', '2/21/2020', '3/09/2020'],
'Fee' : [120, 12, 18, 86, 144]
})
df['Join_Date'] = pd.to_datetime(df['Join_Date'])
df
Here, customer 1 has a yearly subscription fee of $120 between January 2020 and January 2021. I want my data frame to breakdown the fee between 2020-01 and 2020-12 to $10 ($120/12 months) each by displaying each month of that year with the monthly fee ($10).
I tried a bunch of resampling methods, but it didn't work. One example-
def atom(row):
if df.Plan=='Yearly':
return (df.Fee/12)
df.groupby(pd.Grouper(key='Join_Date', freq='1M')).apply(atom)
Expected output for first customer-
Is there another method?