My aim is selecting the rows in which the sum of the column “value” is >1000, considering the cumulative value for each month. For instance client1 reaches that value in month 3, since the sum of value during the first 3 months is 1160, as shown in the target_data. My hints were about playing with group by, I was wondering about also using filter or shift, but I am stacked. In "target_data" I show the desidered result
raw_data = {'client': ['client1','client1','client1','client1','client2','client2','client2','client2','client2'],
'month': [1,2,3,4,1,2,3,4,5],
'value': [10,350,800,400,300,300,300,300,10]}
df = pd.DataFrame(raw_data, columns = ['client','month','value'])
df
target_data = {'client': ['client1','client2'],
'month': [3,4],
'value': [800,300],
'cumulative': [1160,1200]}
df2 = pd.DataFrame(target_data, columns = ['client','month','value','cumulative'])
df2
df_grouped = df.groupby(['client'])['value'].sum().reset_index(name='sum_value')
df_grouped.loc[df_grouped['sum_value'] > 1000]