RowSort
Sorts the rows of a dataframe to either be ascending or descending for one or more columns. If more than one column is specified, the column order that is applied will follow the order given to Arctic Fox. Furtheremore, the order for each column can be specified and can be either ascending or descending.
Options
columns: Specifies columns to sort rows
descending: Specifies whether to sort rows in descending order
ascending: Specifies whether to sort rows in ascending order
order: Specifies the order of sorting rows
Examples
Example 1 - Sort Dataframe by Single Column
We often want to sort a dataframe according to the values in a column. To sort by a single column, simply pass in the column name. If the direction, ascending or descending, is not specified, as in this example, the default direction will be ascending, going from smalles to largest.
#> RowSort --columns High
AFLEFT
appleStockDf = appleStockDf.sort_values(by='High') AFRIGHT
Example 2
Similar to the previous, this example sorts the dataframe on a single column. However, this time we specify the order in which to sort, which is descending in this case. This causes the column to be sorted from largest to smallest.
#> RowSort --columns Volume --order descending -exampleTitle Sort by Single Column and Specify Direction
AFLEFT
appleStockDf = appleStockDf.sort_values(by='Volume', ascending=[False, False, False, False, False, False, False, False]) AFRIGHT
Example 3 - Sort on Multiple Columns
Rather than sorting on a single column, it is not uncommon to want to sort first on one column, and then sort on a second column when the values of the first column are the same. In this example, we first sort on the column High, and second, we sort on the column with index 4 when the column High has the same value. Additionally, we specify to sort High in descending order, and then column with index 4 in ascending order.
#> RowSort --columns High 4 --order descending ascending
AFLEFT
appleStockDf = appleStockDf.sort_values(by= ['High', appleStockDf.columns[4]] , ascending=[False, True]) AFRIGHT