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 sort expression based on the ascending order of the given column name, and null values return before non-null values. Supports Spark Connect.
Syntax
from pyspark.sql import functions as dbf
dbf.asc_nulls_first(col=<col>)
Parameters
| Parameter | Type | Description |
|---|---|---|
col |
pyspark.sql.Column or str |
Target column to sort by in the ascending order. |
Returns
pyspark.sql.Column: the column specifying the order.
Examples
Example 1: Sorting a DataFrame with null values in ascending order.
from pyspark.sql import functions as dbf
df = spark.createDataFrame([(1, "Bob"), (0, None), (2, "Alice")], ["age", "name"])
df.sort(dbf.asc_nulls_first(df.name)).show()
+---+-----+
|age| name|
+---+-----+
| 0| NULL|
| 2|Alice|
| 1| Bob|
+---+-----+
Example 2: Sorting a DataFrame with null values in ascending order using column name string.
from pyspark.sql import functions as dbf
df = spark.createDataFrame([(1, "Bob"), (0, None), (2, "Alice")], ["age", "name"])
df.sort(dbf.asc_nulls_first("name")).show()
+---+-----+
|age| name|
+---+-----+
| 0| NULL|
| 2|Alice|
| 1| Bob|
+---+-----+