回傳一個新的 DataFrame,每個分割區依指定欄位排序。
語法
sortWithinPartitions(*cols: Union[int, str, Column, List[Union[int, str, Column]]], **kwargs: Any)
參數
| 參數 | 類型 | 說明 |
|---|---|---|
cols |
整數、力量、列表或欄位,選用 | 列名或列名列表,供排序。 |
ascending |
bool 或 list,可選,預設 True | 布林值或布林值清單。 排序是往上升還是向下排序。 指定多個排序順序的清單。 若指定一個列表,列表的長度必須等於 的 cols長度。 |
退貨
DataFrame: DataFrame 依分割區排序。
Notes
列序數從 1 開始,這與基於 __getitem__0 的 不同。 若某欄序數為負,則表示排序為下降。
Examples
from pyspark.sql import functions as sf
df = spark.createDataFrame([(2, "Alice"), (5, "Bob")], schema=["age", "name"])
df.sortWithinPartitions("age", ascending=False)
# DataFrame[age: bigint, name: string]
df.coalesce(1).sortWithinPartitions(1).show()
# +---+-----+
# |age| name|
# +---+-----+
# | 2|Alice|
# | 5| Bob|
# +---+-----+
df.coalesce(1).sortWithinPartitions(-1).show()
# +---+-----+
# |age| name|
# +---+-----+
# | 5| Bob|
# | 2|Alice|
# +---+-----+