Pyspark: reshape data without aggregation

后端 未结 2 516
情话喂你
情话喂你 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:16

    Using groupby and pivot is the natural way to do this, but if you want to avoid any aggregation you can achieve this with a filter and join

    import pyspark.sql.functions as f
    
    df.where("value_HIGH = 1").select("FAULTY", f.col("count").alias("value_HIGH_1"))\
        .join(
            df.where("value_HIGH = 0").select("FAULTY", f.col("count").alias("value_HIGH_1")),
            on="FAULTY"
        )\
        .show()
    #+------+------------+------------+
    #|FAULTY|value_HIGH_1|value_HIGH_1|
    #+------+------------+------------+
    #|     0|          12|         140|
    #|     1|          21|         141|
    #+------+------------+------------+
    

提交回复
热议问题