Python Pandas: How to Sort a Pandas DataFrame by Column Names or Row Index
Sorting a DataFrame by its labels - either row index labels or column names - is a common operation for organizing data for display, reporting, or further processing. Unlike sort_values(), which sorts by the data within columns, sort_index() sorts by the axis labels themselves.
This guide explains how to use sort_index() to sort both rows and columns alphabetically, in ascending or descending order, and in place.
Sample DataFrame
All examples use the following DataFrame with non-alphabetical row indices and column names:
import pandas as pd
students = [
('Ethan', 22, 'Texas', 'UCLA'),
('Olivia', 31, 'California', 'USC'),
('Liam', 16, 'New York', 'NYU'),
('Sophia', 41, 'Illinois', 'University of Chicago'),
('Noah', 33, 'Florida', 'University of Florida'),
('Ava', 35, 'Washington', 'University of Washington'),
('Mason', 35, 'Georgia', 'Georgia Tech'),
('Isabella', 35, 'Ohio', 'Ohio State'),
('Lucas', 35, 'Michigan', 'University of Michigan'),
('Mia', 35, 'Arizona', 'Arizona State')
]
df = pd.DataFrame(
students,
columns=['Name', 'Age', 'Place', 'College'],
index=['b', 'c', 'a', 'e', 'f', 'g', 'i', 'j', 'k', 'd']
)
print(df)
Output:
Name Age Place College
b Ethan 22 Texas UCLA
c Olivia 31 California USC
a Liam 16 New York NYU
e Sophia 41 Illinois University of Chicago
f Noah 33 Florida University of Florida
g Ava 35 Washington University of Washington
i Mason 35 Georgia Georgia Tech
j Isabella 35 Ohio Ohio State
k Lucas 35 Michigan University of Michigan
d Mia 35 Arizona Arizona State
Notice the row index labels (b, c, a, e, f, g, i, j, k, d) are not in alphabetical order.
Understanding sort_index() Syntax
DataFrame.sort_index(axis=0, ascending=True, inplace=False, na_position='last')
| Parameter | Description | Default |
|---|---|---|
axis | 0 to sort rows by index labels, 1 to sort columns by column names | 0 |
ascending | True for A→Z / 0→9, False for Z→A / 9→0 | True |
inplace | If True, modifies the DataFrame directly instead of returning a new one | False |
na_position | Where to place NaN values: 'first' or 'last' | 'last' |
Sorting Rows by Index Labels
Ascending Order (Default)
import pandas as pd
students = [
('Ethan', 22, 'Texas', 'UCLA'),
('Olivia', 31, 'California', 'USC'),
('Liam', 16, 'New York', 'NYU'),
('Sophia', 41, 'Illinois', 'University of Chicago'),
('Noah', 33, 'Florida', 'University of Florida'),
('Ava', 35, 'Washington', 'University of Washington'),
('Mason', 35, 'Georgia', 'Georgia Tech'),
('Isabella', 35, 'Ohio', 'Ohio State'),
('Lucas', 35, 'Michigan', 'University of Michigan'),
('Mia', 35, 'Arizona', 'Arizona State')
]
df = pd.DataFrame(
students,
columns=['Name', 'Age', 'Place', 'College'],
index=['b', 'c', 'a', 'e', 'f', 'g', 'i', 'j', 'k', 'd']
)
# sorting in ascending order
sorted_df = df.sort_index()
print(sorted_df)
Output:
Name Age Place College
a Liam 16 New York NYU
b Ethan 22 Texas UCLA
c Olivia 31 California USC
d Mia 35 Arizona Arizona State
e Sophia 41 Illinois University of Chicago
f Noah 33 Florida University of Florida
g Ava 35 Washington University of Washington
i Mason 35 Georgia Georgia Tech
j Isabella 35 Ohio Ohio State
k Lucas 35 Michigan University of Michigan
The rows are now arranged alphabetically by their index labels: a, b, c, d, e, f, g, i, j, k.
Descending Order
import pandas as pd
students = [
('Ethan', 22, 'Texas', 'UCLA'),
('Olivia', 31, 'California', 'USC'),
('Liam', 16, 'New York', 'NYU'),
('Sophia', 41, 'Illinois', 'University of Chicago'),
('Noah', 33, 'Florida', 'University of Florida'),
('Ava', 35, 'Washington', 'University of Washington'),
('Mason', 35, 'Georgia', 'Georgia Tech'),
('Isabella', 35, 'Ohio', 'Ohio State'),
('Lucas', 35, 'Michigan', 'University of Michigan'),
('Mia', 35, 'Arizona', 'Arizona State')
]
df = pd.DataFrame(
students,
columns=['Name', 'Age', 'Place', 'College'],
index=['b', 'c', 'a', 'e', 'f', 'g', 'i', 'j', 'k', 'd']
)
# sorting in descending order
sorted_desc = df.sort_index(ascending=False)
print(sorted_desc)
Output:
Name Age Place College
k Lucas 35 Michigan University of Michigan
j Isabella 35 Ohio Ohio State
i Mason 35 Georgia Georgia Tech
g Ava 35 Washington University of Washington
f Noah 33 Florida University of Florida
e Sophia 41 Illinois University of Chicago
d Mia 35 Arizona Arizona State
c Olivia 31 California USC
b Ethan 22 Texas UCLA
a Liam 16 New York NYU
Sorting in Place
Use inplace=True to modify the original DataFrame directly without creating a copy:
import pandas as pd
students = [
('Ethan', 22, 'Texas', 'UCLA'),
('Olivia', 31, 'California', 'USC'),
('Liam', 16, 'New York', 'NYU'),
('Sophia', 41, 'Illinois', 'University of Chicago'),
('Noah', 33, 'Florida', 'University of Florida'),
('Ava', 35, 'Washington', 'University of Washington'),
('Mason', 35, 'Georgia', 'Georgia Tech'),
('Isabella', 35, 'Ohio', 'Ohio State'),
('Lucas', 35, 'Michigan', 'University of Michigan'),
('Mia', 35, 'Arizona', 'Arizona State')
]
df = pd.DataFrame(
students,
columns=['Name', 'Age', 'Place', 'College'],
index=['b', 'c', 'a', 'e', 'f', 'g', 'i', 'j', 'k', 'd']
)
# sorting in place
df.sort_index(inplace=True)
print(df)
Output:
Name Age Place College
a Liam 16 New York NYU
b Ethan 22 Texas UCLA
c Olivia 31 California USC
d Mia 35 Arizona Arizona State
e Sophia 41 Illinois University of Chicago
f Noah 33 Florida University of Florida
g Ava 35 Washington University of Washington
i Mason 35 Georgia Georgia Tech
j Isabella 35 Ohio Ohio State
k Lucas 35 Michigan University of Michigan
The original df is now sorted. No new DataFrame is returned.
Use inplace=True when you want to permanently reorder the DataFrame and don't need the original order. Otherwise, assign the result to a new variable to keep the original intact.
Sorting Columns by Column Names
To sort columns alphabetically, set axis=1. This rearranges the column order without changing any data values.
Ascending Order
import pandas as pd
students = [
('Ethan', 22, 'Texas', 'UCLA'),
('Olivia', 31, 'California', 'USC'),
('Liam', 16, 'New York', 'NYU'),
('Sophia', 41, 'Illinois', 'University of Chicago'),
('Noah', 33, 'Florida', 'University of Florida'),
('Ava', 35, 'Washington', 'University of Washington'),
('Mason', 35, 'Georgia', 'Georgia Tech'),
('Isabella', 35, 'Ohio', 'Ohio State'),
('Lucas', 35, 'Michigan', 'University of Michigan'),
('Mia', 35, 'Arizona', 'Arizona State')
]
df = pd.DataFrame(
students,
columns=['Name', 'Age', 'Place', 'College'],
index=['b', 'c', 'a', 'e', 'f', 'g', 'i', 'j', 'k', 'd']
)
# sorting in ascending order
sorted_cols = df.sort_index(axis=1)
print(sorted_cols)
Output:
Age College Name Place
b 22 UCLA Ethan Texas
c 31 USC Olivia California
a 16 NYU Liam New York
e 41 University of Chicago Sophia Illinois
f 33 University of Florida Noah Florida
g 35 University of Washington Ava Washington
i 35 Georgia Tech Mason Georgia
j 35 Ohio State Isabella Ohio
k 35 University of Michigan Lucas Michigan
d 35 Arizona State Mia Arizona
The columns are now in alphabetical order: Age, College, Name, Place.
Descending Order
import pandas as pd
students = [
('Ethan', 22, 'Texas', 'UCLA'),
('Olivia', 31, 'California', 'USC'),
('Liam', 16, 'New York', 'NYU'),
('Sophia', 41, 'Illinois', 'University of Chicago'),
('Noah', 33, 'Florida', 'University of Florida'),
('Ava', 35, 'Washington', 'University of Washington'),
('Mason', 35, 'Georgia', 'Georgia Tech'),
('Isabella', 35, 'Ohio', 'Ohio State'),
('Lucas', 35, 'Michigan', 'University of Michigan'),
('Mia', 35, 'Arizona', 'Arizona State')
]
df = pd.DataFrame(
students,
columns=['Name', 'Age', 'Place', 'College'],
index=['b', 'c', 'a', 'e', 'f', 'g', 'i', 'j', 'k', 'd']
)
# sorting in descending order
sorted_cols_desc = df.sort_index(axis=1, ascending=False)
print(sorted_cols_desc)
Output:
Place Name College Age
b Texas Ethan UCLA 22
c California Olivia USC 31
a New York Liam NYU 16
e Illinois Sophia University of Chicago 41
f Florida Noah University of Florida 33
g Washington Ava University of Washington 35
i Georgia Mason Georgia Tech 35
j Ohio Isabella Ohio State 35
k Michigan Lucas University of Michigan 35
d Arizona Mia Arizona State 35
Columns are now in reverse alphabetical order: Place, Name, College, Age.
Sorting Columns in Place
import pandas as pd
students = [
('Ethan', 22, 'Texas', 'UCLA'),
('Olivia', 31, 'California', 'USC'),
('Liam', 16, 'New York', 'NYU'),
('Sophia', 41, 'Illinois', 'University of Chicago'),
('Noah', 33, 'Florida', 'University of Florida'),
('Ava', 35, 'Washington', 'University of Washington'),
('Mason', 35, 'Georgia', 'Georgia Tech'),
('Isabella', 35, 'Ohio', 'Ohio State'),
('Lucas', 35, 'Michigan', 'University of Michigan'),
('Mia', 35, 'Arizona', 'Arizona State')
]
df = pd.DataFrame(
students,
columns=['Name', 'Age', 'Place', 'College'],
index=['b', 'c', 'a', 'e', 'f', 'g', 'i', 'j', 'k', 'd']
)
# sorting in place
df.sort_index(axis=1, inplace=True)
print(df)
Output:
Age College Name Place
b 22 UCLA Ethan Texas
c 31 USC Olivia California
a 16 NYU Liam New York
e 41 University of Chicago Sophia Illinois
f 33 University of Florida Noah Florida
g 35 University of Washington Ava Washington
i 35 Georgia Tech Mason Georgia
j 35 Ohio State Isabella Ohio
k 35 University of Michigan Lucas Michigan
d 35 Arizona State Mia Arizona
Sorting Both Rows and Columns
You can sort both axes by chaining sort_index() calls:
import pandas as pd
students = [
('Ethan', 22, 'Texas', 'UCLA'),
('Olivia', 31, 'California', 'USC'),
('Liam', 16, 'New York', 'NYU'),
('Sophia', 41, 'Illinois', 'University of Chicago'),
('Noah', 33, 'Florida', 'University of Florida'),
('Ava', 35, 'Washington', 'University of Washington'),
('Mason', 35, 'Georgia', 'Georgia Tech'),
('Isabella', 35, 'Ohio', 'Ohio State'),
('Lucas', 35, 'Michigan', 'University of Michigan'),
('Mia', 35, 'Arizona', 'Arizona State')
]
df = pd.DataFrame(
students,
columns=['Name', 'Age', 'Place', 'College'],
index=['b', 'c', 'a', 'e', 'f', 'g', 'i', 'j', 'k', 'd']
)
# Sort rows by index, then columns by name
result = df.sort_index(axis=0).sort_index(axis=1)
print(result)
Output:
Age College Name Place
a 16 NYU Liam New York
b 22 UCLA Ethan Texas
c 31 USC Olivia California
d 35 Arizona State Mia Arizona
e 41 University of Chicago Sophia Illinois
f 33 University of Florida Noah Florida
g 35 University of Washington Ava Washington
i 35 Georgia Tech Mason Georgia
j 35 Ohio State Isabella Ohio
k 35 University of Michigan Lucas Michigan
Both row indices and column names are now in alphabetical order.
Common Mistake: Confusing sort_index() with sort_values()
A frequent error is using sort_index() when you intend to sort by the data within a column, or vice versa:
import pandas as pd
df = pd.DataFrame({
'Name': ['Charlie', 'Alice', 'Bob'],
'Score': [78, 95, 88]
}, index=[2, 0, 1])
# WRONG: sort_index sorts by the index labels (2, 0, 1), not by Name
result = df.sort_index()
print("Sorted by index (not by Name):")
print(result)
Output:
Sorted by index (not by Name):
Name Score
0 Alice 95
1 Bob 88
2 Charlie 78
The rows are sorted by index values (0, 1, 2), not by the Name column alphabetically.
To sort by column values:
import pandas as pd
df = pd.DataFrame({
'Name': ['Charlie', 'Alice', 'Bob'],
'Score': [78, 95, 88]
}, index=[2, 0, 1])
# CORRECT: use sort_values to sort by data in a column
result = df.sort_values(by='Name')
print("Sorted by Name:")
print(result)
Output:
Sorted by Name:
Name Score
0 Alice 95
1 Bob 88
2 Charlie 78
- Use
sort_index()when you want to reorder rows by their index labels or columns by their column names. - Use
sort_values(by='column')when you want to sort rows by the data inside a specific column.
Quick Reference
| Goal | Code |
|---|---|
| Sort rows by index (ascending) | df.sort_index() |
| Sort rows by index (descending) | df.sort_index(ascending=False) |
| Sort columns by name (ascending) | df.sort_index(axis=1) |
| Sort columns by name (descending) | df.sort_index(axis=1, ascending=False) |
| Sort in place | df.sort_index(inplace=True) |
| Sort both rows and columns | df.sort_index().sort_index(axis=1) |
| Sort by column values instead | df.sort_values(by='column_name') |
The sort_index() method provides a clean way to organize your DataFrame by its structural labels - row indices and column names - making your data easier to read, compare, and present.