nxcals.api.extraction.data.builders.DataFrame.sortWithinPartitions

DataFrame.sortWithinPartitions(*cols: int | str | Column | List[int | str | Column], **kwargs: Any) → DataFrame

Returns a new DataFrame with each partition sorted by the specified column(s).

Added in version 1.6.0.

Changed in version 3.4.0: Supports Spark Connect.

Parameters:
  • cols (int, str, list or Column, optional) –

    list of Column or column names or column ordinals to sort by.

    Changed in version 4.0.0: Supports column ordinal.

  • ascending (bool or list, optional, default True) – boolean or list of boolean. Sort ascending vs. descending. Specify list for multiple sort orders. If a list is specified, the length of the list must equal the length of the cols.

Returns:

DataFrame sorted by partitions.

Return type:

DataFrame

Notes

A column ordinal starts from 1, which is different from the 0-based __getitem__(). If a column ordinal is negative, it means sort descending.

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