Pyspark: reshape data without aggregation

后端 未结 2 517
情话喂你
情话喂你 2021-01-16 17:40

I want to reshape my data from 4x3 to 2x2 in pyspark without aggregating. My current output is the following:

co         


        
2条回答
  •  情书的邮戳
    2021-01-16 18:22

    You can use pivot with a fake maximum aggregation (since you have only one element for each group):

    import pyspark.sql.functions as F
    df.groupBy('FAULTY').pivot('value_HIGH').agg(F.max('count')).selectExpr(
        'FAULTY', '`1` as value_high_1', '`0` as value_high_0'
    ).show()
    +------+------------+------------+
    |FAULTY|value_high_1|value_high_0|
    +------+------------+------------+
    |     0|          12|         140|
    |     1|          21|         141|
    +------+------------+------------+
    

提交回复
热议问题