Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/6.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
Apache spark 对行中的唯一值进行计数_Apache Spark_Pyspark_Apache Spark Sql_Row_Unique - Fatal编程技术网

Apache spark 对行中的唯一值进行计数

Apache spark 对行中的唯一值进行计数,apache-spark,pyspark,apache-spark-sql,row,unique,Apache Spark,Pyspark,Apache Spark Sql,Row,Unique,测试数据: df = spark.createDataFrame([(1, 1), (2, 3), (3, 3)], ['c1', 'c2']) df.show() #+---+---+ #| c1| c2| #+---+---+ #| 1| 1| #| 2| 3| #| 3| 3| #+---+---+ 我打算在每个行中对不同的值进行计数,创建一个包含计数的单独列。怎么做 预期结果: #+---+---+---+ #| c1| c2| c3| #+---+---+---+ #|

测试数据:

df = spark.createDataFrame([(1, 1), (2, 3), (3, 3)], ['c1', 'c2'])
df.show()
#+---+---+
#| c1| c2|
#+---+---+
#|  1|  1|
#|  2|  3|
#|  3|  3|
#+---+---+
我打算在每个行中对不同的值进行计数,创建一个包含计数的单独列。怎么做

预期结果:

#+---+---+---+
#| c1| c2| c3|
#+---+---+---+
#|  1|  1|  1|
#|  2|  3|  2|
#|  3|  3|  1|
#+---+---+---+

检查
数组的大小\u distinct

import pyspark.sql.functions as F

df.withColumn('c3', F.size(F.array_distinct(F.array(*df.columns)))).show()
+---+---+---+
| c1| c2| c3|
+---+---+---+
|  1|  1|  1|
|  2|  3|  2|
|  3|  3|  1|
+---+---+---+