我需要在一个新的pct_change列中分别为每个ID计算value列中值的百分比变化。示例df如下。
互联网上的一些消息来源说,在pyspark2.4+中有一个pct_change()函数,这将使这一点变得容易,但我在3.0.1上,我无法从pyspark.sql.functions导入它。
ID value pct_change
1 1 nan
1 2 1
1 4 1
2 1 nan
2 1 0
2 0.5 -0.5
3 5 nan
3 5 0
3 7 0.4
1条答案
按热度按时间vjhs03f71#
在pyspark中使用窗口功能
代码和逻辑如下