sortWithinPartitions

返回一个新的 DataFrame,每个分区都按指定的列排序。

Syntax

sortWithinPartitions(*cols: Union[int, str, Column, List[Union[int, str, Column]]], **kwargs: Any)

参数

参数 类型 说明
cols int、str、list 或 Column,可选 要排序依据的列名或列序号的列表。
ascending bool 或 list、optional、default True 布尔值或布尔值列表。 升序排序与降序。 为多个排序顺序指定列表。 如果指定了列表,则列表的长度必须等于该 cols列表的长度。

退货

DataFrame:按分区排序的数据帧。

备注

列序号从 1 开始,这不同于从 0 开始 __getitem__的 。 如果列序号为负数,则表示降序排序。

示例

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|
# +---+-----+