I have a dataframe that looks like this:
I have appended an aggregated row at the end, that calculates the mean of the column, while ignoring null values. See my code here:
repayments_amt_pivot.loc['Aggregated'] = repayments_amt_pivot.iloc[:, 3:].mean(skipna=True)
However, instead of a simple mean for the aggregated row, I actually need to calculate a weighted sum of the percentages multipled by the proportion in principal_due_per_month.
In this case, for Month 4, row 0 would be multipled by (27,845 / 27,845 + 310,506 + 659,705 + 1,433,121).
For Month 3, row 4 would be multiplied by (1,941,036 / 27,845 + 310,506 + 659,705 + 1,433,121 + 1,941,036)
etc.
Any help would be greatly appreciated as I cannot figure this one out!
See the following for screenshots of how to calculate in excel.
Data:
pd.DataFrame([{'$ Amount Due': 27845.312793586978,
'Month 0': 56.479872661140476,
'Month 1': 92.94027983726657,
'Month 2': 100.00000000000003,
'Month 3': 100.00000000000003,
'Month 4': 100.00000000000003},
{'$ Amount Due': 310505.5597382864,
'Month 0': 78.34839385064039,
'Month 1': 79.58303224427453,
'Month 2': 79.58303224427453,
'Month 3': 81.43498983472573,
'Month 4': 92.54673537743292},
{'$ Amount Due': 659705.2173778547,
'Month 0': 90.79718901057414,
'Month 1': 97.8418387417451,
'Month 2': 97.85768670717538,
'Month 3': 97.85768670717538,
'Month 4': 97.85768670717538},
{'$ Amount Due': 1433121.318250646,
'Month 0': 91.7207168764003,
'Month 1': 94.34283888419282,
'Month 2': 94.51326381568556,
'Month 3': 94.8581612152927,
'Month 4': 94.91544740629973},
{'$ Amount Due': 1941036.1276433321,
'Month 0': 79.75029644420579,
'Month 1': 85.62252846197367,
'Month 2': 86.59251760542142,
'Month 3': 86.70920561577343,
'Month 4': np.nan},
{'$ Amount Due': 3448302.2801859295,
'Month 0': 75.83697471065258,
'Month 1': 83.6700011095642,
'Month 2': 86.16217213969533,
'Month 3': np.nan,
'Month 4': np.nan},
{'$ Amount Due': 3190042.0279137697,
'Month 0': 76.69574360823212,
'Month 1': 85.4625418697537,
'Month 2': np.nan,
'Month 3': np.nan,
'Month 4': np.nan},
{'$ Amount Due': 2614440.2956102462,
'Month 0': 74.87175589142862,
'Month 1': np.nan,
'Month 2': np.nan,
'Month 3': np.nan,
'Month 4': np.nan}])