Python Pandas: How to Reverse the Column Order of a Pandas DataFrame in Python
When working with DataFrames, you may need to reverse the order of columns - for example, to match a required output format, align with another dataset's structure, or simply display data in a different arrangement. Pandas offers several straightforward methods to flip columns from left to right.
In this guide, you'll learn multiple approaches to reverse column order, understand the differences between them, and choose the best method for your use case.
Creating a Sample DataFrame
Let's create a DataFrame to use throughout all examples:
import pandas as pd
data = {
'Name': ['Alice', 'Bob', 'Charlie', 'Diana'],
'Age': [29, 35, 28, 42],
'City': ['New York', 'Chicago', 'Boston', 'Houston'],
'Salary': [75000, 82000, 68000, 91000]
}
df = pd.DataFrame(data)
print("Original DataFrame:")
print(df)
Output:
Original DataFrame:
Name Age City Salary
0 Alice 29 New York 75000
1 Bob 35 Chicago 82000
2 Charlie 28 Boston 68000
3 Diana 42 Houston 91000
The column order is: Name → Age → City → Salary. Our goal is to reverse it to Salary → City → Age → Name.
Method 1: Using iloc[:, ::-1] (Recommended)
The most common and efficient approach uses iloc with Python's slice notation to reverse column positions:
import pandas as pd
data = {
'Name': ['Alice', 'Bob', 'Charlie', 'Diana'],
'Age': [29, 35, 28, 42],
'City': ['New York', 'Chicago', 'Boston', 'Houston'],
'Salary': [75000, 82000, 68000, 91000]
}
df = pd.DataFrame(data)
reversed_df = df.iloc[:, ::-1]
print(reversed_df)
Output:
Salary City Age Name
0 75000 New York 29 Alice
1 82000 Chicago 35 Bob
2 68000 Boston 28 Charlie
3 91000 Houston 42 Diana
How It Works
iloc[:, ::-1]means: select all rows (:) and all columns in reverse order (::-1).- The
::-1slice steps through column positions backward, from the last column to the first.
df.iloc[:, ::-1] is the recommended approach for reversing columns. It's concise, efficient, and preserves all data types correctly. It works identically to how you'd reverse a Python list with list[::-1].
Method 2: Using df.columns[::-1]
You can reverse the column labels and use them to reorder the DataFrame:
import pandas as pd
data = {
'Name': ['Alice', 'Bob', 'Charlie', 'Diana'],
'Age': [29, 35, 28, 42],
'City': ['New York', 'Chicago', 'Boston', 'Houston'],
'Salary': [75000, 82000, 68000, 91000]
}
df = pd.DataFrame(data)
reversed_df = df[df.columns[::-1]]
print(reversed_df)
Output:
Salary City Age Name
0 75000 New York 29 Alice
1 82000 Chicago 35 Bob
2 68000 Boston 28 Charlie
3 91000 Houston 42 Diana
This approach first reverses the df.columns Index object, then uses it to select columns in the new order.
Method 3: Using reindex()
The reindex() method lets you rearrange columns according to a new column order:
import pandas as pd
data = {
'Name': ['Alice', 'Bob', 'Charlie', 'Diana'],
'Age': [29, 35, 28, 42],
'City': ['New York', 'Chicago', 'Boston', 'Houston'],
'Salary': [75000, 82000, 68000, 91000]
}
df = pd.DataFrame(data)
reversed_df = df.reindex(columns=df.columns[::-1])
print(reversed_df)
Output:
Salary City Age Name
0 75000 New York 29 Alice
1 82000 Chicago 35 Bob
2 68000 Boston 28 Charlie
3 91000 Houston 42 Diana
reindex() is more verbose but useful when you want to rearrange columns in any arbitrary order, not just a reversal.
Method 4: Using reversed()
Python's built-in reversed() function creates a reverse iterator over the column labels:
import pandas as pd
data = {
'Name': ['Alice', 'Bob', 'Charlie', 'Diana'],
'Age': [29, 35, 28, 42],
'City': ['New York', 'Chicago', 'Boston', 'Houston'],
'Salary': [75000, 82000, 68000, 91000]
}
df = pd.DataFrame(data)
reversed_df = df[list(reversed(df.columns))]
print(reversed_df)
Output:
Salary City Age Name
0 75000 New York 29 Alice
1 82000 Chicago 35 Bob
2 68000 Boston 28 Charlie
3 91000 Houston 42 Diana
Note that reversed() returns an iterator, so you need to wrap it in list() before using it for column selection.
Method 5: Using Transpose (.T)
A less conventional approach transposes the DataFrame, reverses the rows (which are now the original columns), and transposes back:
import pandas as pd
data = {
'Name': ['Alice', 'Bob', 'Charlie', 'Diana'],
'Age': [29, 35, 28, 42],
'City': ['New York', 'Chicago', 'Boston', 'Houston'],
'Salary': [75000, 82000, 68000, 91000]
}
df = pd.DataFrame(data)
reversed_df = df.T.iloc[::-1].T
print(reversed_df)
Output:
Salary City Age Name
0 75000 New York 29 Alice
1 82000 Chicago 35 Bob
2 68000 Boston 28 Charlie
3 91000 Houston 42 Diana
The transpose method can change data types. When a DataFrame has mixed types (strings, integers, floats), transposing converts all values to a common type (usually object). After transposing back, numeric columns may become strings:
import pandas as pd
data = {
'Name': ['Alice', 'Bob', 'Charlie', 'Diana'],
'Age': [29, 35, 28, 42],
'City': ['New York', 'Chicago', 'Boston', 'Houston'],
'Salary': [75000, 82000, 68000, 91000]
}
df = pd.DataFrame(data)
print("Original dtypes:")
print(df.dtypes)
reversed_df = df.T.iloc[::-1].T
print("\nAfter transpose reversal dtypes:")
print(reversed_df.dtypes)
Output:
Original dtypes:
Name object
Age int64
City object
Salary int64
dtype: object
After transpose reversal dtypes:
Salary object
City object
Age object
Name object
dtype: object
Avoid the transpose method when preserving data types matters. Use iloc[:, ::-1] instead.
Reversing and Resetting Column Names
If you want to reverse the columns and also rename them to a new set of names:
import pandas as pd
df = pd.DataFrame({
'A': [1, 2, 3],
'B': [4, 5, 6],
'C': [7, 8, 9]
})
reversed_df = df.iloc[:, ::-1]
reversed_df.columns = ['X', 'Y', 'Z']
print(reversed_df)
Output:
X Y Z
0 7 4 1
1 8 5 2
2 9 6 3
Reversing Specific Columns Only
Sometimes you only want to reverse a subset of columns while keeping others in place:
import pandas as pd
df = pd.DataFrame({
'ID': [1, 2, 3],
'Name': ['Alice', 'Bob', 'Charlie'],
'Score_1': [85, 90, 78],
'Score_2': [92, 88, 95],
'Score_3': [76, 84, 89]
})
# Keep 'ID' and 'Name' in place, reverse only the score columns
fixed_cols = ['ID', 'Name']
score_cols = ['Score_1', 'Score_2', 'Score_3']
reversed_df = df[fixed_cols + score_cols[::-1]]
print(reversed_df)
Output:
ID Name Score_3 Score_2 Score_1
0 1 Alice 76 92 85
1 2 Bob 84 88 90
2 3 Charlie 89 95 78
Comparison of Methods
| Method | Syntax | Preserves dtypes | Performance | Readability |
|---|---|---|---|---|
iloc[:, ::-1] | df.iloc[:, ::-1] | ✅ | ⭐ Fast | ⭐ Clear |
columns[::-1] | df[df.columns[::-1]] | ✅ | ⭐ Fast | ⭐ Clear |
reindex() | df.reindex(columns=df.columns[::-1]) | ✅ | ⭐ Fast | Good |
reversed() | df[list(reversed(df.columns))] | ✅ | ⭐ Fast | Good |
Transpose .T | df.T.iloc[::-1].T | ❌ | Slower | Less intuitive |
Summary
To reverse the column order of a Pandas DataFrame:
- Use
df.iloc[:, ::-1]- the most concise, efficient, and type-safe method. This is the recommended default. - Use
df[df.columns[::-1]]- equally effective and readable. - Use
reindex()- useful when you need more flexibility in rearranging columns. - Avoid the transpose method (
.T[::-1].T) unless you're certain all columns share the same data type, as it can silently change data types.
All methods (except transpose) return a new DataFrame without modifying the original, and all correctly preserve data types and values.