Open In App

Change the order of a Pandas DataFrame columns in Python

Improve
Improve
Like Article
Like
Save
Share
Report

When expanding Pandas dataframes with extra columns, the structure may become unwieldy, and the column arrangement might lose its coherence. To enhance the readability of your dataframes, it’s common to reorder the columns, placing them in a more logical sequence. In this article, we are going to see how to change the order of dataframe columns in Python.

Change the Order of a Pandas DataFrame Columns

Below are the ways by which we can change the order of a Pandas DataFrame Columns in Python:

  • Using iloc method
  • Using loc method
  • Using a subset of columns by passing a list
  • Using Reverse methods

Changing the Order of a Pandas DataFrame Column Using iloc methods

Here, we are using iloc methods, we will pass the different indexes in the iloc to change the order of dataframe columns. In this example, a Pandas DataFrame is initially created with columns ‘Sr.no’, ‘Name’, and ‘Maths Score’. The original DataFrame is displayed, and then a new DataFrame is printed by selecting columns in a different order: ‘Sr.no’, ‘Maths Score’, and ‘Name’.

Python3




# importing the modules
import pandas as pd
import numpy as np
 
# creating the DataFrame
my_data = {'Sr.no': [1, 2, 3, 4, 5],
           'Name': ['Ram', 'Sham', 'Sonu',
                    'Tinu', 'Monu'],
           'Maths Score': [45, 67, 89, 74, 56]}
df = pd.DataFrame(data = my_data)
 
# printing the original DataFrame
print("My Original DataFrame")
display(df)
 
# printing the new DataFrame
print("My new DataFrame")
 
df.iloc[:,[0,2,1]]


Output:

My Original DataFrame
Sr.no Name Maths Score
0 1 Ram 45
1 2 Sham 67
2 3 Sonu 89
3 4 Tinu 74
4 5 Monu 56
My new DataFrame
Sr.no Maths Score Name
0 1 45 Ram
1 2 67 Sham
2 3 89 Sonu
3 4 74 Tinu
4 5 56 Monu

Change the Order of a Pandas DataFrame Columns in Python Using loc method

Here, we will pass the columns name with loc. In this example, a Pandas DataFrame is created with columns ‘Sr.no’, ‘Name’, and ‘Maths Score’. The original DataFrame is displayed, and a new DataFrame is printed by selecting columns in a different order: ‘Maths Score’, ‘Name’, and ‘Sr.no’, using the loc function.

Python3




# importing the modules
import pandas as pd
import numpy as np
 
# creating the DataFrame
my_data = {'Sr.no': [1, 2, 3, 4, 5],
           'Name': ['Ram', 'Sham', 'Sonu',
                    'Tinu', 'Monu'],
           'Maths Score': [45, 67, 89, 74, 56]}
df = pd.DataFrame(data = my_data)
 
# printing the original DataFrame
print("My Original DataFrame")
display(df)
 
# printing the new DataFrame
print("My new DataFrame")
 
df.loc[:,['Maths Score','Name','Sr.no']]


Output:

My Original DataFrame
Sr.no Name Maths Score
0 1 Ram 45
1 2 Sham 67
2 3 Sonu 89
3 4 Tinu 74
4 5 Monu 56
My new DataFrame
Maths Score Name Sr.no
0 45 Ram 1
1 67 Sham 2
2 89 Sonu 3
3 74 Tinu 4
4 56 Monu 5

Python Change the Order of a Pandas Column Using a Subset of Columns by Passing a List

In this example, a Pandas DataFrame is created with columns ‘Sr.no’, ‘Name’, and ‘Maths Score’. The original DataFrame is then altered to rearrange columns, placing ‘Sr.no’, ‘Maths Score’, and ‘Name’ in a new order.

Python3




# importing the modules
import pandas as pd
import numpy as np
 
# creating the DataFrame
my_data = {'Sr.no': [1, 2, 3, 4, 5],
           'Name': ['Ram', 'Sham', 'Sonu',
                    'Tinu', 'Monu'],
           'Maths Score': [45, 67, 89, 74, 56]}
df = pd.DataFrame(data=my_data)
 
# printing the original DataFrame
print("My Original DataFrame")
print(df)
 
# altering the DataFrame
df = df[['Sr.no', 'Maths Score', 'Name']]
 
# printing the altered DataFrame
print('After altering Name and Maths Score')
print(df)


Output: 

My Original DataFrame
Sr.no Name Maths Score
0 1 Ram 45
1 2 Sham 67
2 3 Sonu 89
3 4 Tinu 74
4 5 Monu 56
After altering Name and Maths Score
Sr.no Maths Score Name
0 1 45 Ram
1 2 67 Sham
2 3 89 Sonu
3 4 74 Tinu
4 5 56 Monu

In this example, a Pandas DataFrame is created with columns ‘Name’, ‘Code’, ‘Age’, and ‘Weight’. The original DataFrame is then modified to reorder columns, placing ‘Name’, ‘Code’, ‘Weight’, and ‘Age’ in a new sequence.

Python3




# importing the modules
import pandas as pd
 
# creating the DataFrame
l1 = ["Amar", "Barsha", "Carlos", "Tanmay", "Misbah"]
l2 = ["Alpha", "Bravo", "Charlie", "Tango", "Mike"]
l3 = [23, 25, 22, 27, 29]
l4 = [69, 54, 73, 70, 74]
df = pd.DataFrame(list(zip(l1, l2, l3, l4)))
df.columns = ['Name', 'Code', 'Age', 'Weight']
 
# printing the original DataFrame
print("My Original DataFrame")
print(df)
 
# altering the DataFrame
df = df[['Name', 'Code', 'Weight', 'Age']]
 
# printing the altered DataFrame
print('After altering Weight and Age')
print(df)


Output:

My Original DataFrame
Name Code Age Weight
0 Amar Alpha 23 69
1 Barsha Bravo 25 54
2 Carlos Charlie 22 73
3 Tanmay Tango 27 70
4 Misbah Mike 29 74
After altering Weight and Age
Name Code Weight Age
0 Amar Alpha 69 23
1 Barsha Bravo 54 25
2 Carlos Charlie 73 22
3 Tanmay Tango 70 27
4 Misbah Mike 74 29

Change the Order of DataFrame Columns Using List Reverse methods

Here, we will use list reverse() methods to change the order of the columns. In this example, a Pandas DataFrame is created with columns ‘Sr.no’, ‘Name’, and ‘Maths Score’. The original DataFrame is displayed, and a new DataFrame is printed by reversing the order of columns: ‘Maths Score’, ‘Name’, and ‘Sr.no’.

Python3




# importing the modules
import pandas as pd
import numpy as np
 
# creating the DataFrame
my_data = {'Sr.no': [1, 2, 3, 4, 5],
           'Name': ['Ram', 'Sham', 'Sonu',
                    'Tinu', 'Monu'],
           'Maths Score': [45, 67, 89, 74, 56]}
df = pd.DataFrame(data = my_data)
 
# printing the original DataFrame
print("My Original DataFrame")
display(df)
 
cols = list(df.columns)
cols.reverse()
 
# printing the new DataFrame
print("My new DataFrame")
 
df[cols]


Output:

My Original DataFrame
Sr.no Name Maths Score
0 1 Ram 45
1 2 Sham 67
2 3 Sonu 89
3 4 Tinu 74
4 5 Monu 56
My new DataFrame
Maths Score Name Sr.no
0 45 Ram 1
1 67 Sham 2
2 89 Sonu 3
3 74 Tinu 4
4 56 Monu 5


Last Updated : 05 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads