Saturday, November 16, 2024
Google search engine
HomeLanguagesChange the order of a Pandas DataFrame columns in Python

Change the order of a Pandas DataFrame columns in Python

In this article, we are going to see how to change the order of dataframe columns in Python.

Different ways to 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

Method 1: 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.

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:

 

Method 2: Using loc method

Here we will pass the columns name with loc.

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:

 

Method 3: Using a subset of columns by passing a list

Example 1 :

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: 

 

 

Example 2 :

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 : 
 

 

 

Method 4: Using List Reverse methods

Here we will use list reverse() methods to change the order of the columns.

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:

 

RELATED ARTICLES

Most Popular

Recent Comments