将行的子集转换为列 pyspark 数据框

Convert subset of rows to column pyspark dataframe

假设我们有以下df

Id PlaceCod  Val
1  1         0 
1  2         3
2  2         4 
2  1         5
3  1         6

如何将这个 DF 转换成这个:

Id Store Warehouse
1  0     3
2  5     4
3  6     null

我尝试使用 df.pivot(f.col("PlaceCod")) 但收到错误消息 'DataFrame has no pivot attribute'

正如@Emma 在评论中发表的那样:

df.groupby('Id').pivot('PlaceCod').agg(F.first('Val'))

使用上面的方案我的问题就解决了!