Saturday, December 6, 2025
HomeLanguagesHow to reverse the column order of the Pandas DataFrame?

How to reverse the column order of the Pandas DataFrame?

Sometimes when working with DataFrames we might want to change or reverse the order of the column of the dataframe. In this article, let’s see how to reverse the order of the columns of a dataframe. This can be achieved in two ways –

Method 1: The sequence of columns appearing in the dataframe can be reversed by using the attribute.columns[::-1] on the corresponding dataframe. It accesses the columns from the end and outer dataframe[…] reindexes the dataframe using this new sequence provided. 

Example:

Python3




# importing required modules
import pandas as pd
 
 
dataframe = pd.DataFrame([[1, 'A', "Student"],
                          [2, 'B', "Tutor"],
                          [3, 'C', "Instructor"]])
 
print("Original DataFrame")
display(dataframe)
 
# reversing the dataframe
print("Reversed DataFrame")
display(dataframe[dataframe.columns[::-1]])


Output:

Method 2: iloc indexer can also be used to reverse the column order of the data frame, using the syntax iloc[:, ::-1] on the specified dataframe. The contents are not preserved in the original dataframe. 

Python3




# importing required modules
import pandas as pd
 
 
dataframe = pd.DataFrame([[1, 'A', "Student"],
                          [2, 'B', "Tutor"],
                          [3, 'C', "Instructor"]])
 
print("Original DataFrame")
display(dataframe)
 
# reversing the dataframe
print("Reversed DataFrame")
display(dataframe.iloc[:, ::-1])


Output:

RELATED ARTICLES

Most Popular

Dominic
32427 POSTS0 COMMENTS
Milvus
98 POSTS0 COMMENTS
Nango Kala
6802 POSTS0 COMMENTS
Nicole Veronica
11944 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12012 POSTS0 COMMENTS
Shaida Kate Naidoo
6932 POSTS0 COMMENTS
Ted Musemwa
7185 POSTS0 COMMENTS
Thapelo Manthata
6881 POSTS0 COMMENTS
Umr Jansen
6867 POSTS0 COMMENTS