Lưu ý
Cần có ủy quyền mới truy nhập được vào trang này. Bạn có thể thử đăng nhập hoặc thay đổi thư mục.
Cần có ủy quyền mới truy nhập được vào trang này. Bạn có thể thử thay đổi thư mục.
Returns a new Column for distinct count of col or cols.
Syntax
from pyspark.sql import functions as sf
sf.count_distinct(col, *cols)
Parameters
| Parameter | Type | Description |
|---|---|---|
col |
pyspark.sql.Column or column name |
First column to compute on. |
cols |
pyspark.sql.Column or column name |
Other columns to compute on. |
Returns
pyspark.sql.Column: distinct values of these two column values.
Examples
Example 1: Counting distinct values of a single column
from pyspark.sql import functions as sf
df = spark.createDataFrame([(1,), (1,), (3,)], ["value"])
df.select(sf.count_distinct(df.value)).show()
+---------------------+
|count(DISTINCT value)|
+---------------------+
| 2|
+---------------------+
Example 2: Counting distinct values of multiple columns
from pyspark.sql import functions as sf
df = spark.createDataFrame([(1, 1), (1, 2)], ["value1", "value2"])
df.select(sf.count_distinct(df.value1, df.value2)).show()
+------------------------------+
|count(DISTINCT value1, value2)|
+------------------------------+
| 2|
+------------------------------+
Example 3: Counting distinct values with column names as strings
from pyspark.sql import functions as sf
df = spark.createDataFrame([(1, 1), (1, 2)], ["value1", "value2"])
df.select(sf.count_distinct("value1", "value2")).show()
+------------------------------+
|count(DISTINCT value1, value2)|
+------------------------------+
| 2|
+------------------------------+