Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 通过消除空值在ApacheSpark中合并行_Python_Scala_Apache Spark_Pyspark_Apache Spark Sql - Fatal编程技术网

Python 通过消除空值在ApacheSpark中合并行

Python 通过消除空值在ApacheSpark中合并行,python,scala,apache-spark,pyspark,apache-spark-sql,Python,Scala,Apache Spark,Pyspark,Apache Spark Sql,我有一个spark数据框,如下所示 +---+----+----+----+----+----+----+ | id| 1| 2| 3|sf_1|sf_2|sf_3| +---+----+----+----+----+----+----+ | 2|null|null|null| 102| 202| 302| | 4|null|null|null| 104| 204| 304| | 1|null|null|null| 101| 201| 301| | 3|null|null|n

我有一个spark数据框,如下所示

+---+----+----+----+----+----+----+
| id|   1|   2|   3|sf_1|sf_2|sf_3|
+---+----+----+----+----+----+----+
|  2|null|null|null| 102| 202| 302|
|  4|null|null|null| 104| 204| 304|
|  1|null|null|null| 101| 201| 301|
|  3|null|null|null| 103| 203| 303|
|  1|  11|  21|  31|null|null|null|
|  2|  12|  22|  32|null|null|null|
|  4|  14|  24|  34|null|null|null|
|  3|  13|  23|  33|null|null|null|
+---+----+----+----+----+----+----+
我想通过合并空行来转换数据帧,如下所示

+---+----+----+----+----+----+----+
| id|   1|   2|   3|sf_1|sf_2|sf_3|
+---+----+----+----+----+----+----+
|  1|  11|  21|  31| 101| 201| 301|
|  2|  12|  22|  32| 102| 202| 302|
|  4|  14|  24|  34| 104| 204| 304|
|  3|  13|  23|  33| 103| 203| 303|
+---+----+----+----+----+----+----+
最好是在scala中。

您可以在id上分组,并对其他列使用first with ignorenulls进行聚合:

import pyspark.sql.functions as F

(df.groupBy('id').agg(*[F.first(x,ignorenulls=True) for x in df.columns if x!='id'])
.show())
您可以使用first with ignorenulls对其他列进行id分组和聚合:

import pyspark.sql.functions as F

(df.groupBy('id').agg(*[F.first(x,ignorenulls=True) for x in df.columns if x!='id'])
.show())
这是一种做事的方式

val inputColumns = inputLoadDF.columns.toList.drop(0)
val exprs = inputColumns.map(x => first(x,true))
inputLoadDF.groupBy("id").agg(exprs.head,exprs.tail:_*).show()
这是一种做事的方式

val inputColumns = inputLoadDF.columns.toList.drop(0)
val exprs = inputColumns.map(x => first(x,true))
inputLoadDF.groupBy("id").agg(exprs.head,exprs.tail:_*).show()

我们能做这个scala吗?什么是F?@Learnis我没有安装scala,但应该非常相似,因为函数在scala中是相似的。F是我导入模块的方式。好的,Np@anky。谢谢我补充说,这是另一个答案。接受你除了这个还有更好的方法吗?因为groupBy是一个非常繁重的操作,我有大约7kcolumns@Learnis我不认为我们可以避免分组,但可能会有不同的方法。因为这已经结束了,所以最好发布一个关于可见性的新问题。我们可以使用scala吗?什么是F?@Learnis我没有安装scala,但应该非常相似,因为函数在scala中是相似的。F是我导入模块的方式。好的,Np@anky。谢谢我补充说,这是另一个答案。接受你除了这个还有更好的方法吗?因为groupBy是一个非常繁重的操作,我有大约7kcolumns@Learnis我不认为我们可以避免分组,但可能会有不同的方法。这是一个更好的发布一个新的问题的可见性,因为这是关闭。