How to change the order of DataFrame columns?
Reordering columns in a Pandas DataFrame is a common data-wrangling task. Below are several straightforward ways to do it, along with tips and best practices.
1. Select Columns by Subsetting
One of the most direct and common methods is simply reassigning your DataFrame to a subset of its columns in the order you want:
import pandas as pd df = pd.DataFrame({ 'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9] }) # Reorder columns to ['C', 'A', 'B'] df = df[['C', 'A', 'B']] print(df)
This creates a new DataFrame (or reassigns the same variable) with columns in the desired order.
- Pros: Very explicit and easy to read.
- Cons: You must list all the columns in the new order, which can be tedious with many columns.
2. Using reindex()
with columns
Parameter
Alternatively, you can use the reindex()
method:
df = df.reindex(columns=['C', 'A', 'B'])
- Pros: Consistent with other Pandas operations (like reindexing rows).
- Cons: Similar to subsetting, you need to specify the full list of columns.
3. Assigning a New Column Order to df.columns
You can reorder columns by directly modifying the df.columns
attribute:
df.columns = ['C', 'A', 'B']
- Pros: A one-liner that renames columns and reorders them (if you keep the same names, it effectively reorders).
- Cons: Risk of mistakes if you accidentally rename columns. Also, you must match the existing column count and names precisely.
4. Sorting Columns Alphabetically
If you want to sort columns by their names (e.g., alphabetically):
df = df.reindex(sorted(df.columns), axis=1)
- Pros: Quick and handy if your columns follow a naming convention that sorts logically (e.g., alphabetical or numeric).
- Cons: You might lose your original manual ordering logic if it’s not purely alphabetical or numeric.
5. Inserting or Moving Columns One-by-One
If you need to move or insert columns individually rather than rewriting the entire list:
# Example: Move column 'B' to the first position col = df.pop('B') # Remove and return column 'B' df.insert(0, 'B', col) # Insert at index 0
- Pros: Useful if you have a large DataFrame and just want to move one or two columns without rewriting them all.
- Cons: More steps than a simple column reindex if you’re moving many columns at once.
Which Method Should You Use?
-
Small DataFrames or when you know the final column order:
- Reassign with the list of columns in the desired order (Method 1), or
reindex()
(Method 2).
- Reassign with the list of columns in the desired order (Method 1), or
-
One-off Moves:
- Use
pop()
andinsert()
to move specific columns.
- Use
-
Sorting Columns:
- Use
df.reindex(sorted(df.columns), axis=1)
.
- Use
Regardless of the method you choose, remember that columns in a DataFrame are just labels. Reordering them doesn’t change the underlying data—only how it’s displayed and accessed.
Further Resources to Boost Your Python & Data Skills
If you’re interested in going beyond basic data manipulation, here are some high-value resources from DesignGurus.io:
-
Grokking Python Fundamentals
Perfect for deepening your Python skills, covering best practices, object-oriented programming, and more advanced topics. -
Grokking the Coding Interview: Patterns for Coding Questions
Ideal if you’re preparing for coding interviews—learn the critical patterns you’ll encounter, many of which can be tackled with Python’s versatile toolset.
For engineers aiming to excel in system design and distributed systems, consider:
- Grokking System Design Fundamentals
A hands-on course that breaks down the complexities of scalable system architectures, data flows, and performance tuning—essential for senior developer or architect roles.
Final Thoughts
Reordering columns in a Pandas DataFrame is straightforward:
- Subset with the desired column list.
- Use
reindex(columns=[...])
. - Directly manipulate
df.columns
or utilize thepop()
+insert()
pattern for one-off moves.
Choose the approach that best fits your workflow and code readability preferences. By combining these data manipulation techniques with a solid understanding of Python fundamentals, you’ll be well on your way to building robust, maintainable data processing pipelines.
Happy Data Wrangling!