我使用以下代码来汇总每年的学生。目的是了解每年的学生总数。
from pyspark.sql.functions import col
import pyspark.sql.functions as fn
gr = Df2.groupby(['Year'])
df_grouped =
gr.agg(fn.count(col('Student_ID')).alias('total_student_by_year'))
问题是我发现很多ID重复,所以结果错误而且巨大。
我想按年份聚合学生,按年份统计学生总数,避免ID重复。
最佳答案
使用countDistinct函数
from pyspark.sql.functions import countDistinct
x = [("2001","id1"),("2002","id1"),("2002","id1"),("2001","id1"),("2001","id2"),("2001","id2"),("2002","id2")]
y = spark.createDataFrame(x,["year","id"])
gr = y.groupBy("year").agg(countDistinct("id"))
gr.show()
输出
+----+------------------+
|year|count(DISTINCT id)|
+----+------------------+
|2002| 2|
|2001| 2|
+----+------------------+
关于python - 如何在pyspark中计算groupBy后的唯一ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46421677/