Reorder the columns of pandas dataframe in Python

In this post, we will see 3 different methods to Reordering the columns of Pandas Dataframe :
[toc]

Using reindex method

You can use DataFrame’s reindex() method to reorder columns of pandas DataFrame.

You need to pass columns=[$list_of_columns] to reindex() method to reorder columns of Pandas DataFrame.

# import pandas package as pd in this code
import pandas as pd

# make a dictionary containing students data
data = {
'Name': ['Ankit', 'Aishwarya', 'Shaurya', 'Shivangi'],
'Gender' : ['M','F','M','F'],
'Age': [23, 21, 22, 21],
'Course': ['B.tech', 'B.tech', 'B.sc', 'B.sc'],
'Branch': ['Cse','Cse','It','It'],
'College': ['Geu','Geu','Gehu','Gehu'],
}

# Convert the given dictionary into pandas DataFrame
df = pd.DataFrame(data, columns = ['Name','Gender', 'Age', 'Course','Branch', 'College'])

# print the pandas Dataframe
print("Given Dataframe :\n", df)

# reordering of the columns using reindex()
rslt_df = df.reindex(columns= ['Name', 'Age','Gender' ,'College','Course','Branch' ])

# print the pandas DataFrame
print("\nDataframe after Re-ordering of columns :\n", rslt_df)

Output :

Given Dataframe :
Name Gender Age Course Branch College
0 Ankit M 23 B.tech Cse Geu
1 Aishwarya F 21 B.tech Cse Geu
2 Shaurya M 22 B.sc It Gehu
3 Shivangi F 21 B.sc It Gehu

Dataframe after Re-ordering of columns :
Name Age Gender College Course Branch
0 Ankit 23 M Geu B.tech Cse
1 Aishwarya 21 F Geu B.tech Cse
2 Shaurya 22 M Gehu B.sc It
3 Shivangi 21 F Gehu B.sc It

Using column selection through column name

We can use column selection through column name of DataFrame to reorder the column.

Here is an example:

# import pandas package as pd in this code
import pandas as pd

# make a dictionary containing students data
data = {
'Name': ['Ankit', 'Aishwarya', 'Ankita', 'Swapnil'],
'Gender' : ['M','F','F','M'],
'Age': [23, 21, 22, 21],
'Course': ['B.tech', 'B.tech', 'B.sc', 'B.sc'],
'Branch': ['Cse','Cse','It','It'],
'College': ['Geu','Geu','Gehu','Gehu'],
}

# Convert the given dictionary into pandas DataFrame
df = pd.DataFrame(data, columns = ['Name','Gender', 'Age', 'Course','Branch', 'College'])

# print the pandas Dataframe
print("Given Dataframe :\n", df)

# reordering of the columns by using column selection
rslt_df = df[['Name','Gender' ,'Age','College','Course','Branch' ]]

# print the pandas DataFrame
print("\nDataframe after Re-ordering of columns :\n", rslt_df)

Output :

Given Dataframe :
Name Gender Age Course Branch College
0 Ankit M 23 B.tech Cse Geu
1 Aishwarya F 21 B.tech Cse Geu
2 Ankita F 22 B.sc It Gehu
3 Swapnil M 21 B.sc It Gehu

Dataframe after Re-ordering of columns :
Name Gender Age College Course Branch
0 Ankit M 23 Geu B.tech Cse
1 Aishwarya F 21 Geu B.tech Cse
2 Ankita F 22 Gehu B.sc It
3 Swapnil M 21 Gehu B.sc It

Using column selection through column index

We can use column selection through column index of DataFrame to reorder the column.

Here is an example:

# import pandas package as pd in this code
import pandas as pd

# make a dictionary containing students data
data = {
'Name': ['Ankit', 'Aishwarya', 'Ankita', 'Swapnil'],
'Gender' : ['M','F','F','M'],
'Age': [23, 21, 22, 21],
'Course': ['B.tech', 'B.tech', 'B.sc', 'B.sc'],
'Branch': ['Cse','Cse','It','It'],
'College': ['Geu','Geu','Gehu','Gehu'],
}

# Convert the given dictionary into pandas DataFrame
df = pd.DataFrame(data, columns = ['Name','Gender', 'Age', 'Course','Branch', 'College'])

# print the pandas Dataframe
print("Given Dataframe :\n", df)

# reordering of the columns by use of column index.
rslt_df = df[df.columns[[5,4,3,2,1,0]]]

# print the pandas DataFrame
print("\nDataframe after Re-ordering of columns :\n", rslt_df)

Output :

Given Dataframe :
Name Gender Age Course Branch College
0 Ankit M 23 B.tech Cse Geu
1 Aishwarya F 21 B.tech Cse Geu
2 Ankita F 22 B.sc It Gehu
3 Swapnil M 21 B.sc It Gehu

Dataframe after Re-ordering of columns :
College Branch Course Age Gender Name
0 Geu Cse B.tech 23 M Ankit
1 Geu Cse B.tech 21 F Aishwarya
2 Gehu It B.sc 22 F Ankita
3 Gehu It B.sc 21 M Swapnil

That’s all about how to reorder the columns of pandas dataframe in Python.

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.