Reorder the columns of pandas dataframe in Python

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

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.

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:

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:

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.

Was this post helpful?

Leave a Reply

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