Sorting columns in pandas dataframe based on column name
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Sorting pandas columns by column name is simple once you remember that columns are just an index on axis 1. The main question is not whether pandas can do it, but what kind of ordering you want: alphabetical, reverse alphabetical, or a custom business order. Pandas supports all of those, but the cleanest method depends on the case.
Sort Columns Alphabetically
The simplest built-in method is sort_index on axis 1.
This sorts the column labels in ascending lexical order.
Equivalent result using reindex:
Both are fine. sort_index(axis=1) is usually the clearest when the goal is plain label sorting.
Reverse the Sort Order
If you want descending order:
This is useful for quick inspection, though business-oriented reporting usually prefers an explicit custom order instead of reverse alphabetical sorting.
Apply a Custom Column Order
Often the real requirement is not alphabetical order. It is "put these important columns first."
This is better than sorting when the desired sequence is domain-driven rather than lexical.
Sort by a Derived Key
Sometimes column names contain numbers or prefixes and normal string sorting is not what you want.
Example problem:
- '
col1' - '
col10' - '
col2'
Lexical sorting puts col10 before col2. If you want natural numeric ordering, sort with a custom key.
That gives col1, col2, col10, which is often what users expect.
Sort Only a Subset of Columns First
Another useful pattern is "bring some columns to the front and sort the rest automatically."
This is a good compromise for data export workflows where a few columns are important and the rest can be alphabetized.
MultiIndex Columns Need Special Handling
If your DataFrame has MultiIndex columns, sort_index(axis=1) still works, but the sort operates on tuple-like labels. That is sometimes correct and sometimes surprising.
In that case, inspect df.columns first and decide whether you want:
- full tuple sorting
- sorting by one level only
- a manually defined order
The method is the same, but the intent needs to be explicit.
Common Pitfalls
- Using plain string sorting when column names contain numbers such as
col1,col2, andcol10. - Reordering with a manual list and forgetting one required column.
- Assuming alphabetical order is the same as business-friendly order.
- Forgetting that columns are axis
1, not axis0. - Applying
sort_valueswhen the goal was to sort column labels rather than row data.
Summary
- Use
df.sort_index(axis=1)for simple alphabetical column sorting. - Use
ascending=Falsefor reverse order. - Use a manual list when the required order is business-driven.
- Use a custom key when column names contain embedded numbers or patterns.
- Keep in mind that sorting columns means working on axis
1, not sorting row values.
Related reading
- Spark Dataframe write to kafka topic in avro format?
- Spark Find pairs having at least n common attributes?
- Spark K-fold Cross Validation
- Spark MLlib / K-Means intuition
- sorting efficiently
- Sorting HashMap by values
- Sorting list according to corresponding values from a parallel list
- Spark 3.x Integration with Kafka in Python

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.