Open In App

How to reverse the column order of the Pandas DataFrame?

Last Updated : 19 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Let's learn how to reverse the column order of the Pandas DataFrame using iloc and other approaches.

Using Slicing - columns[::-1]

The columns[::-1] slices the column labels in reverse order. Let's consider an example:

Python
import pandas as pd

# Sample DataFrame
data = {
    'Name': ['Rachel', 'Monica', 'Phoebe', 'Joey', 'Chandler', 'Ross'],
    'Age': [29, 30, 31, 28, 32, 33], 
    'Occupation': ['Fashion Executive', 'Chef', 'Musician', 'Actor', 'Advertiser', 'Paleontologist']

    }

df = pd.DataFrame(data)

print("Original DataFrame:")
print(df)

reversed_df = df[df.columns[::-1]]

print("Reversed DataFrame (using columns[::-1]):")
print(reversed_df)

Output:

Original DataFrame:
Name Age Occupation
0 Rachel 29 Fashion Executive
1 Monica 30 Chef
2 Phoebe 31 Musician
3 Joey 28 Actor
4 Chandler 32 Advertiser
5 Ross 33 Paleontologist
Reversed DataFrame (using columns[::-1]):
Occupation Age Name
0 Fashion Executive 29 Rachel
1 Chef 30 Monica
2 Musician 31 Phoebe
3 Actor 28 Joey
4 Advertiser 32 Chandler
5 Paleontologist 33 Ross

Reversing Column Order using iloc[] with Slicing

iloc[] method slices the DataFrame by columns using positional indexing.

Python
reversed_df = df.iloc[:, ::-1]

print("Reversed DataFrame (Using iloc with Slicing):")
print(reversed_df)

Output:

reversed-dataframe
Reversed DataFrame (Using iloc with Slicing)

The iloc method and columns[::-1] are the most common methods used to reverse the column order of a Pandas DataFrame. Apart from these, there are also other methods available that can achieve the same outcome:

Using reversed() to reverse column order in Pandas DataFrame

The reversed() function creates a reverse iterator for the column labels.

Python
reversed_df = df[list(reversed(df.columns))]

print("Reversed DataFrame (using reversed()):")
print(reversed_df)

Output:

Reversed DataFrame (using reversed()):
Occupation Age Name
0 Fashion Executive 29 Rachel
1 Chef 30 Monica
2 Musician 31 Phoebe
3 Actor 28 Joey
4 Advertiser 32 Chandler
5 Paleontologist 33 Ross

Using reindex()

Reindexing (using reindex() method) allows you to rearrange the columns in a reversed order.

Python
reversed_df = df.reindex(columns=df.columns[::-1])

print("Reversed DataFrame (using reindex()):")
print(reversed_df)

Output:

Reversed DataFrame (using reindex()):
Occupation Age Name
0 Fashion Executive 29 Rachel
1 Chef 30 Monica
2 Musician 31 Phoebe
3 Actor 28 Joey
4 Advertiser 32 Chandler
5 Paleontologist 33 Ross

Using T (Transpose)

Transpose the DataFrame, reverse the rows (now columns), and transpose it back.

Python
reversed_df = df.T[::-1].T

print("Reversed DataFrame (Using T (Transpose)):")
print(reversed_df)

Output:

Reversed DataFrame (Using T (Transpose)):
Occupation Age Name
0 Fashion Executive 29 Rachel
1 Chef 30 Monica
2 Musician 31 Phoebe
3 Actor 28 Joey
4 Advertiser 32 Chandler
5 Paleontologist 33 Ross

Each of these methods achieves the same result: reversing the column order of a Pandas DataFrame. Here’s a quick summary of the different approaches:

  • The columns[::-1] method is the most straightforward and efficient for reversing columns.
  • Use iloc if you prefer positional slicing.
  • Both reversed() and reindex() offer clean, alternative approaches.
  • While T transpose works, it is less commonly used for this purpose due to its complexity.

Next Article

Similar Reads