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?


import_contacts

You may also like:

Related Posts

  • Pandas apply function to column
    12 January

    Pandas apply function to column

    Table of ContentsHow do I apply function to column in pandas?Using dataframe.apply() functionUsing lambda function along with the apply() functionUsing dataframe.transform() functionUsing map() functionUsing NumPy.square() functionWas this post helpful? We make use of the Pandas dataframe to store data in an organized and tabular manner. Sometimes there, is a need to apply a function over […]

  • Find rows with nan in Pandas
    11 January

    Find rows with nan in Pandas

    Table of ContentsWhat is nan values in Pandas?Find rows with NAN in pandasFind columns with nan in pandasFind rows with nan in Pandas using isna and iloc()Was this post helpful? In this post, we will see how to find rows with nan in Pandas. What is nan values in Pandas? A pandas DataFrame can contain […]

  • 05 October

    Pandas replace values in column

    Table of ContentsUsing the loc() function to replace values in column of pandas DataFrameUsing the iloc() function to to replace values in column of pandas DataFrameUsing the map() function to replace values of a column in a pandas DataFrameUsing the replace() function to replace values in column of pandas DataFrameUsing the where() function to replace […]

  • Select rows by multiple conditions using loc in Pandas
    29 July

    Pandas Loc Multiple Conditions

    💡 Outline Here is the code to select rows by pandas Loc multiple conditions. [crayon-62e05a8e29a94467611628/] Here, we are select rows of DataFrame where age is greater than 18 and name is equal to Jay. [crayon-62e05a8e29a9a020519943/] The loc() function in a pandas module is used to access values from a DataFrame based on some labels. It […]

  • Split dataframe in Pandas
    28 July

    Split dataframe in Pandas

    Table of ContentsUsing the iloc() function to split DataFrame in PythonBy RowsBy ColumnsUsing the sample() function to split DataFrame in PythonUsing the groupby() function to split DataFrame in PythonUsing the columns to split DataFrame in PythonWas this post helpful? In real-life scenarios, we deal with massive datasets with many rows and columns. At times, we […]

  • Read text file in Pandas
    28 July

    Read text file in Pandas

    Table of ContentsUsing the read_csv() function to read text files in PandasUsing the read_table() function to read text files in PandasUsing the read_fwf() function to read text files in PandasWas this post helpful? A dataset has the data neatly arranged in rows and columns. The pandas module in Python allows us to load DataFrames from […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.