How except one value from a sum? Pandas

I have this df and I want to add a new column "Sum" with the values of the sum bellow the column "Pij" with the same "periodo" and same "i"

 Periodo i  j       Pij           
0   1    1  1   0.298507    
1   1    1  2   0.253731    
2   1    1  3   0.447761    
3   1    2  2   0.843170    
4   1    2  1   0.134907    
5   1    2  3   0.021922    

The result should be

 Periodo i  j       Pij      Sum    
0   1    1  1   0.298507    0.7014
1   1    1  2   0.253731    0.4477  
2   1    1  3   0.447761    0
3   1    2  2   0.843170    0.8650
4   1    2  1   0.134907    0.0219
5   1    2  3   0.021922    0

I have tried a groupby(["Periodo","i"]).cumsum()


Solution 1:

First change order of all rows (swap DataFrame rows by indexing in DataFrame.iloc), then use lambda function in GroupBy.transform with Series.shift and Series.cumsum and last again swap to original order by iloc:

df['Sum'] = (df.iloc[::-1]
               .groupby(["Periodo","i"])['Pij']
               .transform(lambda x: x.shift(fill_value=0).cumsum())
               .iloc[::-1])
print (df)
   Periodo  i  j       Pij       Sum
0        1  1  1  0.298507  0.701492
1        1  1  2  0.253731  0.447761
2        1  1  3  0.447761  0.000000
3        1  2  2  0.843170  0.156829
4        1  2  1  0.134907  0.021922
5        1  2  3  0.021922  0.000000