[英]Calculate percentage change at every variation of a column
I need to calculate the df['col4']= df.pct_change()
of col2
at every variation of col3
.我需要在col3
的每个变化中计算col2
的df['col4']= df.pct_change()
。 I calculate by hand col4
in order to explain better my problem.我用手计算col4
以便更好地解释我的问题。 Thanks谢谢
DataCreazione DataCreazione | col2 col2 | col3 col3 | col4 col4 |
---|---|---|---|
2021-02-17 00:00:00 2021-02-17 00:00:00 | 10.55 10.55 | 0 0 | 0 0 |
2021-02-17 00:00:00 2021-02-17 00:00:00 | 12.55 12.55 | 0 0 | 0.1895 0.1895 |
2021-10-05 00:00:00 2021-10-05 00:00:00 | 14.55 14.55 | 0 0 | 0.1593 0.1593 |
2021-10-05 00:00:00 2021-10-05 00:00:00 | 10.55 10.55 | 0 0 | -0.2749 -0.2749 |
2021-09-15 00:00:00 2021-09-15 00:00:00 | 9.45 9.45 | 1 1 | 0 0 |
2021-10-02 00:00:00 2021-10-02 00:00:00 | 8.65 8.65 | 1 1 | -0.08465 -0.08465 |
2021-10-05 00:00:00 2021-10-05 00:00:00 | 8.65 8.65 | 1 1 | 0 0 |
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
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.