sort() method

Sorts rows in a dataframe into an ascending or descending sequential order, based on a specified key column or columns.

Syntax

dataframe_name.sort(on = ["key_column", "...n"], ascending = True|False)

Parameters

Name Description
on = ["key_column", "...n"]

The key column or columns to use for sorting.

If you sort by more than one column, you create a nested sort. The order of nesting follows the order in which you specify the columns.

Key columns are positioned leftmost in the sorted dataframe.

ascending = True | False

optional

  • True sort the key column or columns in ascending order
  • False sort the key column or columns in descending order

If you omit the parameter, the default ascending order is used.

Returns

HCL dataframe.

Examples

Sort on a single column

You want to sort the rows in the inventory dataframe by product number. The inventory dataframe is permanently reordered:

inventory.sort(on = ["ProdNo"])

Sort on a single column, output entire rows

You want to sort the rows in the inventory dataframe by product number. The sorted rows are output to a new dataframe called inventory_sorted. The order of the original inventory dataframe is left unchanged.

Entire rows are included in the output dataframe:

inventory_sorted = inventory.sort(on = ["ProdNo"])

To switch from the default ascending sort order to a descending sort order, you use the ascending parameter:

inventory_sorted = inventory.sort(on = ["ProdNo"], ascending = False)

Sort on a single column, output a subset of columns

You want to sort the rows in the inventory dataframe by product number. Only the columns specified by the select() method are output to a new dataframe called inventory_quantity_on_hand.

inventory_quantity_on_hand = inventory.sort(on = ["ProdNo"], ascending = True).select(["ProdNo", "ProdDesc", "ProdStat", "QtyOH"])

Sort on multiple columns (nested sort)

You want to sort the rows in the inventory dataframe by location, then by product class, and then by product number. The sorted rows are output to a new dataframe called inventory_location_prodcls_number.

inventory_location_prodcls_number = inventory.sort(on = ["Location", "ProdCls", "ProdNo"], ascending = True)