7
edf.select("x").distinct.show()
顯示出存在於x
列的edf
DataFrame中的不同值。如何統計列中每個不同值的出現次數?
有沒有一種有效的方法來顯示這些不同值在數據框中出現的次數? (計數爲每個不同的值)
edf.select("x").distinct.show()
顯示出存在於x
列的edf
DataFrame中的不同值。如何統計列中每個不同值的出現次數?
有沒有一種有效的方法來顯示這些不同值在數據框中出現的次數? (計數爲每個不同的值)
countDistinct
可能是第一選擇:
import org.apache.spark.sql.functions.countDistinct
df.agg(countDistinct("some_column"))
如果速度比精度更重要的,你可以考慮approxCountDistinct
:
import org.apache.spark.sql.functions.approx_count_distinct
df.agg(approxCountDistinct("some_column"))
爲了獲取值和計數:
df.groupBy("some_column").count()
在SQL中(spark-sql
):
SELECT COUNT(DISTINCT some_column) FROM df
和
SELECT approx_count_distinct(some_column) FROM df
df.select("some_column").distinct.count
這是否告訴你,每一個不同的值怎麼算?我認爲這會告訴你,你有X值,而不是Val1有A,Val2有B,ValX有C? –