sort_index() Method | Teradata Python Package - sort_index() Method - Teradata Package for Python

Teradata® Package for Python User Guide

Deployment
VantageCloud
VantageCore
Edition
Enterprise
IntelliFlex
VMware
Product
Teradata Package for Python
Release Number
20.00
Published
December 2024
ft:locale
en-US
ft:lastEdition
2025-01-23
dita:mapPath
nvi1706202040305.ditamap
dita:ditavalPath
plt1683835213376.ditaval
dita:id
rkb1531260709148
lifecycle
latest
Product Category
Teradata Vantage

Use the sort_index() method to get object sorted by labels (along an axis) in either ascending or descending order for a teradataml DataFrame.

Example 1: Default behavior of sort_index() when no arguments is given.

>>> load_example_data("dataframe","scale_housing_test")
>>> df = DataFrame.from_table('scale_housing_test')
>>> df
          id    price  lotsize  bedrooms  bathrms  stories
types                                                    
classic   14  36000.0   2880.0       3.0      1.0      1.0
bungalow  11  90000.0   7200.0       3.0      2.0      1.0
classic   15  37000.0   3600.0       2.0      1.0      1.0
classic   13  27000.0   1700.0       3.0      1.0      2.0
classic   12  30500.0   3000.0       2.0      1.0      1.0
>>> df.sort_index()
          id    price  lotsize  bedrooms  bathrms  stories
types                                                    
bungalow  11  90000.0   7200.0       3.0      2.0      1.0
classic   13  27000.0   1700.0       3.0      1.0      2.0
classic   12  30500.0   3000.0       2.0      1.0      1.0
classic   14  36000.0   2880.0       3.0      1.0      1.0
classic   15  37000.0   3600.0       2.0      1.0      1.0

Example 2: Use sort_index() with DESCENDING for respective axis.

>>> load_example_data("dataframe","scale_housing_test")
>>> df = DataFrame.from_table('scale_housing_test')
>>> df.sort_index(1, False)
          stories    price  lotsize  id  bedrooms  bathrms
types                                                    
classic       1.0  36000.0   2880.0  14       3.0      1.0
bungalow      1.0  90000.0   7200.0  11       3.0      2.0
classic       1.0  37000.0   3600.0  15       2.0      1.0
classic       2.0  27000.0   1700.0  13       3.0      1.0
classic       1.0  30500.0   3000.0  12       2.0      1.0

Example 3: Use sort_index() with type of sorting algorithm.

>>> load_example_data("dataframe","scale_housing_test")
>>> df = DataFrame.from_table('scale_housing_test')
>>> df.sort_index(1, True, 'mergesort')
          bathrms  bedrooms  id  lotsize    price  stories
types                                                    
classic       1.0       3.0  14   2880.0  36000.0      1.0
bungalow      2.0       3.0  11   7200.0  90000.0      1.0
classic       1.0       2.0  15   3600.0  37000.0      1.0
classic       1.0       3.0  13   1700.0  27000.0      2.0
classic       1.0       2.0  12   3000.0  30500.0      1.0