Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.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中的agg(计数)不工作_Apache Spark_Pyspark - Fatal编程技术网

Apache spark Apache Spark中的agg(计数)不工作

Apache spark Apache Spark中的agg(计数)不工作,apache-spark,pyspark,Apache Spark,Pyspark,正在尝试使用聚合在Apache Spark(PySpark)中对我的数据帧执行聚合 +----+---+---+ |name|age| id| +----+---+---+ |Mark| 4| 1| |Mark| 4| 2| |Mark| 5| 3| |Mark| 5| 4| |Mark| 5| 5| |Mark| 6| 6| |Mark| 8| 7| +----+---+---+ 我有以下代码,可以为一行提供不同的记录计数: old_table.groupby('

正在尝试使用聚合在Apache Spark(PySpark)中对我的数据帧执行聚合

+----+---+---+
|name|age| id|
+----+---+---+
|Mark|  4|  1|
|Mark|  4|  2|
|Mark|  5|  3|
|Mark|  5|  4|
|Mark|  5|  5|
|Mark|  6|  6|
|Mark|  8|  7|
+----+---+---+
我有以下代码,可以为一行提供不同的记录计数:

old_table.groupby('name').agg(countDistinct('age'))
我试图添加一个正常计数作为聚合的另一个输出,但它抛出了一个错误:

old_table.groupby('name').agg(countDistinct('age'), count('age))
错误:

NameError: name 'count' is not defined
有没有办法将count添加到输出的distinct count中,这样我就有了一个如下所示的输出表

+----+-------------+-----+
|name|countDistinct|count|
+----+-------------+-----+
|Mark|            4|    7|
+----+-------------+-----+

您使用的是内置的函数“count”,它需要一个iterable对象,而不是列名

您需要从
pyspark.sql.functions

from pyspark.sql.functions import count as _count

old_table.groupby('name').agg(countDistinct('age'), _count('age'))

你能告诉我们你要进口什么吗?我要进口什么包?这就是你的意思吗?是的。我猜想您只需要从pyspark.sql.functions导入count执行
。但是我建议像导入pyspark.sql.functions为F
这样的东西,然后分别使用
F.count
F.countDistinct
。这很完美,我刚刚检查了一下,这就是我缺少的,我忘记导入count函数了,谢谢!