I want to calculate YTD using pandas dataframe in each month. Here I have used two measurements named sales and sales Rate. For measurement sales, YTD is calculated by taking the cumulative sum.Code is given below:
report_table['ytd_value'] = report_table.groupby(['financial_year', 'measurement', 'place', 'market', 'product'], sort=False)['value'].cumsum()
But, In the case of measurement sales rate YTD is calculated in different way.
YTD Calculation Explanation (sales rate) given below:
First month (April) YTD value of financial year = First month (April) value of financial year
From second month of financial year onwards YTD valueis calculated using formula.
Month May YTD value = ((APRIL YTD value(sales)* APRIL YTD value(sales rate)) + (APRIL value(sales)* APRIL value(sales rate)) / (APRIL value(sales) + APRIL value(sales rate)
Similarly for other months.Dataframe is given below as an image.

import pandas as pd
data = {'Month': ['April', 'May', 'April', 'June', 'April', 'May'],
'Year': [2022, 2022, 2022, 2022, 2022, 2022],
'Financial_Year': [2023, 2023, 2023, 2023, 2023, 2023],
'Measurement': ['sales', 'sales', 'sales', 'sales', 'sales rate', 'sales rate'],
'Place': ['Delhi', 'Delhi', 'Delhi', 'Delhi', 'Delhi', 'Delhi'],
'Market': ['Domestic', 'Domestic', 'Export', 'Domestic', 'Domestic', 'Domestic'],
'Product': ['Biscuit', 'Biscuit', 'Chocolate', 'Biscuit', 'Biscuit', 'Biscuit'],
'Value': ['10', '10', '20', '25', '10', '20']}
# Create DataFrame
df = pd.DataFrame(data)
df['Value'] = df['Value'].astype(float)
df['ytd_value'] = df.groupby(['Financial_Year', 'Measurement', 'Place', 'Market', 'Product'], sort=False)['Value'].cumsum()
It will calculate ytd_value for both sales and sales rate measurement.But I want to calculate ytd_value for sales rate in the above mentioned format.
I have tried below code, but it shows an error:
rslt_df = df[(df['Measurement'] == 'sales')]
df.loc[df['Measurement'] == "sales rate", 'ytd_value'] = (df.groupby(['Financial_Year', 'Measurement', 'Place', 'Market', 'Product'], sort=False)['ytd_value']*rslt_df.groupby(['Financial_Year', 'Measurement', 'Place', 'Market', 'Product'], sort=False)['ytd_value'] + df.groupby(['Financial_Year', 'Measurement', 'Place', 'Market', 'Product'], sort=False)['Value'] * rslt_df.groupby(['Financial_Year', 'Measurement', 'Place', 'Market', 'Product'], sort=False)['Value']) / (rslt_df.groupby(['Financial_Year', 'Measurement', 'Place', 'Market', 'Product'], sort=False)['ytd_value'] + rslt_df.groupby(['Financial_Year', 'Measurement', 'Place', 'Market', 'Product'], sort=False)['Value'])
Expected output:
Month Year Financial_Year ... Product Value ytd_value
0 April 2022 2023 ... Biscuit 10.0 10.0
1 May 2022 2023 ... Biscuit 10.0 20.0
2 April 2022 2023 ... Chocolate 20.0 20.0
3 June 2022 2023 ... Biscuit 25.0 45.0
4 April 2022 2023 ... Biscuit 10.0 10.0
5 May 2022 2023 ... Biscuit 20.0 10.0
Can anyone help me to solve this caclculation?