I have a dataframe like below
stock__stock__FINCODE quantity price transcation_type month
0 100112 1000.0 2000.0 buy 2022-06-01
1 100112 500.0 2000.0 buy 2022-06-01
2 100112 1200.0 3000.0 sell 2022-08-01
3 100112 100.0 3000.0 sell 2022-08-01
4 100112 200.0 3000.0 sell 2022-08-01
5 100325 100.0 1500.0 buy 2022-06-01
I did groupby with stock__stock__FINCODE, transcation_type and month, and made sum of quantity. The code and result is as below
cols = ['stock__stock__FINCODE', 'month', 'transcation_type']
df2 = df.groupby(cols)[['quantity']].sum().add_suffix('_sum')
quantity_sum
stock__stock__FINCODE month transcation_type
100112 2022-06-01 buy 1500.0
2022-08-01 sell 1500.0
100325 2022-06-01 buy 100.0
but I want the result on each month and if a sell happens in same month then need to substract with the buy. If any month is not available then add that month and make the quantity same as previous month.
the result should be some thing like below.
stock__stock__FINCODE 2022-06-01 2022-07-01 2022-08-01
100112 1500 1500 0
100325 100 100 100
the date column is basically monthly quantity. It should start from the smallest date to till now.
can someone please help me with this?