I have a created a new column 'twelve_wk_usage' using a filter, and the groupby and transform functions. The 'twelve_wk_usage' field is essentially summing up usage by unique id if the date corresponding to the usage falls 12 weeks before the maximum date in the dataset.
Code for the fields mentioned below:
df_DTO2['twelve_wks_prior_TF'] = df_DTO2['request_week_end_day
'] == df_DTO2['12_wks_prior']
df_DTO2['twelve_wk_usage'] = df_DTO2[df_DTO2.twelve_wks_prior_TF == True
].groupby(['unique_id'])['sum_ni'].transform(sum))
I am trying to create a new column which divides the current usage by the twelve week usage, but it is not working. I am only getting Nans. Code below:
df_DTO2['current_div_twelve'] = ((df_DTO2['current_wk_usage'])/(
df_DTO2['twelve_wk_usage']))
It seems the only way I can get it to work is to recreate the referenced fields entirely from scratch in the new field. I understand this is because the indexes aren't aligned but am not sure how I can create usable fields with standardized indexes.
I have tried including "twelve_wks_prior_TF" in my grouping statement but it is still summing all usage not just the usage from twelve weeks prior.
Any ideas?