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.

Related Posts

  • Copy DataFrame in Python
    10 July

    Copy DataFrame in Pandas

    This articles provide different ways to copy DataFrame in Pandas.

  • Pandas convert column to int
    18 June

    Pandas convert column to int

    Table of ContentsUse the to_numeric() function to convert column to intUse the astype() function to convert column to intUse the infer_objects() function to convert column to intUse the convert_dtypes() function to convert column to int Pandas is a library set up on top of the Python programming language and is mostly used for the purpose […]

  • 08 September

    Pandas create Dataframe from Dictionary

    Table of ContentsUsing a Dataframe() method of pandas.Using DataFrame.from_dict() method. In this tutorial, We will see different ways of Creating a pandas Dataframe from Dictionary . Using a Dataframe() method of pandas. Example 1 : When we only pass a dictionary in DataFrame() method then it shows columns according to ascending order of their names […]

  • 08 September

    Pandas Convert list to DataFrame

    In this tutorial, We will see different ways of Creating a pandas Dataframe from List. You can use Dataframe() method of pandas library to convert list to DataFrame. so first we have to import pandas library into the python file using import statement. So let’s see the various examples on creating a Dataframe with the […]

  • 26 May

    Pandas DataFrame to CSV

    Table of ContentsSyntax of DataFrame.to_csv()DataFrame sample dataConvert pandas DataFrame to csv Save CSV file without index column Save CSV file with Custom columns header Save CSV file with selected columns Save CSV file with custom delimiter Save CSV file without header Append data to CSV file Save CSV file with index_label In this post, we […]

  • 26 May

    Pandas convert column to float

    Table of ContentsConvert String column to float in PandasUsing asType(float) methodUsing toNumeric() methodConvert Integer column to float in PandasUsing asType(float) methodUsing toNumeric() method In this post, we will see how to convert column to float in Pandas. Convert String column to float in Pandas There are two ways to convert String column to float in […]

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.