【发布时间】:2020-02-07 15:20:22
【问题描述】:
我想知道是否有任何简单的方法可以在 Pyspark 中将多行合并为一个,我是 Python 和 Spark 的新手,并且大部分时间都在使用 Spark.sql。
这是一个数据示例:
id count1 count2 count3
1 null 1 null
1 3 null null
1 null null 5
2 null 1 null
2 1 null null
2 null null 2
预期的输出是:
id count1 count2 count3
1 3 1 5
2 1 1 2
我多次使用 spark SQL 加入它们,想知道是否有更简单的方法。
谢谢!
【问题讨论】:
-
我不确定这是否是有意的,但在您的数据中,看起来每个 id 的列只有一个非空值?
-
如果每个id只有一个非空值,你可以用
ignorenulls =True做groupBy+first。比如:df.groupBy('id').agg(*[first(c, True).alias(c) for c in df.columns[1:]]) -
或
groupBy与max:f.groupBy("id").agg(*[max(c).alias(c) for c in df.columns[1:]]).show()... -
是的,只有一个空值。谢谢大家,我试试看!
标签: apache-spark pyspark pyspark-sql