sortWithinPartitions

指定した列で並べ替えられた各パーティションを含む新しい DataFrame を返します。

構文

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

パラメーター

パラメーター タイプ 説明
cols int、str、list、または Column(省略可能) 並べ替える列または列名または列の序数の一覧。
ascending bool または list、省略可能、既定値 True ブール値またはブール値のリスト。 昇順と降順で並べ替えます。 複数の並べ替え順序のリストを指定します。 リストを指定する場合、リストの長さは colsの長さと同じである必要があります。

返品

DataFrame: パーティションで並べ替えられた 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|
# +---+-----+