首页 > 解决方案 > 在 PySpark Dataframe 中结合旋转和分组聚合

问题描述

我正在重新使用PySpark Dataframe 中组合旋转和聚合列中的示例

假设我有一个 Spark 数据框

 date      | recipe | percent | volume
----------------------------------------
2019-01-01 |   A    |  0.03   |  53
2019-01-01 |   A    |  0.02   |  55
2019-01-01 |   B    |  0.05   |  60
2019-01-02 |   A    |  0.11   |  75
2019-01-02 |   B    |  0.06   |  64
2019-01-02 |   B    |  0.08   |  66

如何像以下伪代码一样以一列为中心并在另一列上聚合:

df.groupBy('date').max('volume').alias('max_volume').pivot('recipe').agg(avg('percent').alias('percent')).show()

 date      | A_percent | B_percent | max_volume
--------------------------------------------------------
2019-01-01 |   0.025   |  0.05     |  60
2019-01-02 |   0.11    |  0.07     |  75

我想一步实现这一点,而不必稍后对列 A_volume 和 B_volume 执行 max 以避免命名这些列。

附言。当我本机运行该伪代码时,我得到

AttributeError: 'DataFrame' object has no attribute 'pivot'

标签: pythonpysparkpivot-tableaggregateetl

解决方案


尝试这个:

from pyspark.sql.functions import *
from pyspark.sql import Window
var win = Window.partitionBy("date") 
data.withColumn("max_vol",max("volume").over(win)).groupBy("date","max_vol") .pivot("recipe") .agg(avg("percent")).show()

推荐阅读