Home > OS >  Calculate percentage change at every variation of a column
Calculate percentage change at every variation of a column

Time:08-10

I need to calculate the df['col4']= df.pct_change() of col2 at every variation of col3. I calculate by hand col4 in order to explain better my problem. Thanks

DataCreazione col2 col3 col4
2021-02-17 00:00:00 10.55 0 0
2021-02-17 00:00:00 12.55 0 0.1895
2021-10-05 00:00:00 14.55 0 0.1593
2021-10-05 00:00:00 10.55 0 -0.2749
2021-09-15 00:00:00 9.45 1 0
2021-10-02 00:00:00 8.65 1 -0.08465
2021-10-05 00:00:00 8.65 1 0

CodePudding user response:

Let's try

df['out'] = (df.groupby('col3', group_keys=False)
             .apply(lambda g: g['col2'].pct_change())
             .fillna(0))
print(df)

         DataCreazione   col2  col3     col4       out
0  2021-02-17 00:00:00  10.55     0  0.00000  0.000000
1  2021-02-17 00:00:00  12.55     0  0.18950  0.189573
2  2021-10-05 00:00:00  14.55     0  0.15930  0.159363
3  2021-10-05 00:00:00  10.55     0 -0.27490 -0.274914
4  2021-09-15 00:00:00   9.45     1  0.00000  0.000000
5  2021-10-02 00:00:00   8.65     1 -0.08465 -0.084656
6  2021-10-05 00:00:00   8.65     1  0.00000  0.000000

CodePudding user response:

I got similar desire like this:

df['col5'] = df.groupby(by=['col3']).col2.pct_change().fillna(0)


DataCreazione            col2  col3     col4      col5
0  2021-02-17 00:00:00  10.55     0         0  0.000000
1  2021-02-17 00:00:00  12.55     0    0.1895  0.189573
2  2021-10-05 00:00:00  14.55     0    0.1593  0.159363
3  2021-10-05 00:00:00  10.55     0   -0.2749 -0.274914
4  2021-09-15 00:00:00   9.45     1         0  0.000000
5  2021-10-02 00:00:00   8.65     1  -0.08465 -0.084656
6  2021-10-05 00:00:00   8.65     1         0  0.000000
  • Related