0
0
PandasHow-ToBeginner · 3 min read

How to Use sort_index in pandas for Sorting DataFrames

Use sort_index() in pandas to sort a DataFrame or Series by its index labels. You can sort in ascending or descending order and choose to sort rows or columns by setting parameters like axis and ascending.
📐

Syntax

The sort_index() method sorts a pandas DataFrame or Series by its index labels.

  • axis: Choose 0 or 'index' to sort rows, 1 or 'columns' to sort columns.
  • ascending: Set True for ascending order (default), False for descending.
  • inplace: If True, modifies the object directly without returning a new one.
  • kind: Sorting algorithm, usually left as default.
  • na_position: Position of missing values, 'last' or 'first'.
python
DataFrame.sort_index(axis=0, ascending=True, inplace=False, kind='mergesort', na_position='last')
💻

Example

This example shows how to sort a DataFrame by its row index in ascending and descending order, and how to sort columns by their labels.

python
import pandas as pd

# Create a sample DataFrame with unordered index and columns
data = {'B': [4, 3, 2], 'A': [1, 5, 6]}
index = ['c', 'a', 'b']
df = pd.DataFrame(data, index=index)

# Sort rows by index ascending
sorted_rows = df.sort_index()

# Sort rows by index descending
sorted_rows_desc = df.sort_index(ascending=False)

# Sort columns by column labels
sorted_columns = df.sort_index(axis=1)

print('Original DataFrame:\n', df)
print('\nSorted rows ascending:\n', sorted_rows)
print('\nSorted rows descending:\n', sorted_rows_desc)
print('\nSorted columns ascending:\n', sorted_columns)
Output
Original DataFrame: B A c 4 1 a 3 5 b 2 6 Sorted rows ascending: B A a 3 5 b 2 6 c 4 1 Sorted rows descending: B A c 4 1 b 2 6 a 3 5 Sorted columns ascending: A B c 1 4 a 5 3 b 6 2
⚠️

Common Pitfalls

One common mistake is forgetting that sort_index() returns a new sorted object by default and does not change the original DataFrame unless inplace=True is set.

Another is mixing up sorting rows (axis=0) and columns (axis=1).

python
import pandas as pd

df = pd.DataFrame({'A': [2, 1]}, index=['b', 'a'])

# Wrong: expecting original df to be sorted without assignment or inplace
sorted_df = df.sort_index()
print('Original df after sort_index without inplace:')
print(df)

# Right: assign back or use inplace
# Option 1: assign
sorted_df = df.sort_index()
print('\nSorted df assigned:')
print(sorted_df)

# Option 2: inplace

df.sort_index(inplace=True)
print('\nOriginal df after inplace sort:')
print(df)
Output
Original df after sort_index without inplace: A b 2 a 1 Sorted df assigned: A a 1 b 2 Original df after inplace sort: A a 1 b 2
📊

Quick Reference

Here is a quick summary of key sort_index() parameters:

ParameterDescriptionDefault
axis0 or 'index' to sort rows, 1 or 'columns' to sort columns0
ascendingSort order: True for ascending, False for descendingTrue
inplaceModify original object if True, else return new sorted objectFalse
na_positionPosition of NaNs: 'last' or 'first''last'
kindSorting algorithm: 'quicksort', 'mergesort', 'heapsort''mergesort'

Key Takeaways

Use sort_index() to sort DataFrame or Series by index labels easily.
Remember sort_index() returns a new object unless inplace=True is set.
Set axis=0 to sort rows and axis=1 to sort columns by their labels.
Use ascending=False to sort in descending order.
Handle missing values position with na_position parameter.