How To Change Order of Columns in Pandas Dataframe – With Examples

Pandas dataframe is a two-dimensional data structure that allows you to store data in rows and columns format.

You can change the order of columns in the pandas dataframe using the df.reindex() method.

In this tutorial, you’ll learn how to change the order of columns in a pandas dataframe.

If You’re in Hurry…

To change the order of columns in pandas using the reindex(), pass the columns list in the order you want.

df = df.reindex(columns=['Available_Since_Date','Product_name', 'Unit_Price','No_Of_Units','Available_Quantity'])

df

The column Available_Since_date is moved to the first position.

Dataframe Will Look Like

Available_Since_DateProduct_nameUnit_PriceNo_Of_UnitsAvailable_Quantity
011/5/2021Keyboard500.00055
14/23/2021Mouse200.00056
208/21/2021Monitor5000.2351010
309/18/2021CPU10000.55020Not Available
409/18/2021CPU10000.55020Not Available
501/05/2021Speakers250.5008NaT
6NaTNaTNaNNaTNaT

If You Want to Understand Details, Read on…

In this tutorial, you’ll learn the different methods available to reorder columns in the dataframe.

Sample Dataframe

Create a sample dataframe that you’ll use throughout this tutorial.

Column Order

  1. Product_name
  2. Unit_Price
  3. No_Of_Units
  4. Available_Quantity
  5. Available_Since_Date

Code

import pandas as pd

data = {"Product_name":["Keyboard","Mouse", "Monitor", "CPU","CPU", "Speakers",pd.NaT],
        "Unit_Price":[500,200, 5000.235, 10000.550, 10000.550, 250.50,None],
        "No_Of_Units":[5,5, 10, 20, 20, 8,pd.NaT],
        "Available_Quantity":[5,6,10,"Not Available","Not Available", pd.NaT,pd.NaT],
        "Available_Since_Date":['11/5/2021', '4/23/2021', '08/21/2021','09/18/2021','09/18/2021','01/05/2021',pd.NaT]
       }

df = pd.DataFrame(data)

df

Dataframe Will Look Like

Product_nameUnit_PriceNo_Of_UnitsAvailable_QuantityAvailable_Since_Date
0Keyboard500.0005511/5/2021
1Mouse200.000564/23/2021
2Monitor5000.235101008/21/2021
3CPU10000.55020Not Available09/18/2021
4CPU10000.55020Not Available09/18/2021
5Speakers250.5008NaT01/05/2021
6NaTNaNNaTNaTNaT

Now let’s see the different methods available to reorder the columns.

Using Reindex

You can change the order of the dataframe columns using the reindex() method.

  • The reindex() method accepts columns as a list.
  • Pass the columns as a list in the order of how you want to rearrange them.

Code

df = df.reindex(columns=['Available_Since_Date','Product_name', 'Unit_Price','No_Of_Units','Available_Quantity'])

df

Dataframe Will Look Like

The column Available_Since_Date is moved to the first position, as in the order specified for the columns attribute.

Available_Since_DateProduct_nameUnit_PriceNo_Of_UnitsAvailable_Quantity
011/5/2021Keyboard500.00055
14/23/2021Mouse200.00056
208/21/2021Monitor5000.2351010
309/18/2021CPU10000.55020Not Available
409/18/2021CPU10000.55020Not Available
501/05/2021Speakers250.5008NaT
6NaTNaTNaNNaTNaT

Using Dataframe Indexing

You can use the dataframe indexing to change the order of the columns.

  • Use the column names in a specific order with df
  • Create a new dataframe out of it to rearrange the columns.

Code

df = df[['Available_Since_Date','Product_name', 'Unit_Price','No_Of_Units','Available_Quantity']]

df

Dataframe Will Look Like

The dataframe columns are rearranged in the order passed for the dataframe indexing.

Available_Since_DateProduct_nameUnit_PriceNo_Of_UnitsAvailable_Quantity
011/5/2021Keyboard500.00055
14/23/2021Mouse200.00056
208/21/2021Monitor5000.2351010
309/18/2021CPU10000.55020Not Available
409/18/2021CPU10000.55020Not Available
501/05/2021Speakers250.5008NaT
6NaTNaTNaNNaTNaT

This is how you can rearrange columns using dataframe indexing.

Sorting Columns Alphabetically Using Reindex()

To sort the columns alphabetically using reindex(),

  1. Fetch the existing columns for the dataframe using df.columns
  2. Pass it to the sorted() method. It’ll sort the methods alphabetically
  3. Pass the sorted value to the reindex() method
  4. Specify axis=1 to denote that the column axes need to be reordered

Code

df = df.reindex(sorted(df.columns), axis=1)

df

Dataframe Will Look Like

The dataframe columns are sorted alphabetically as below.

Available_QuantityAvailable_Since_DateNo_Of_UnitsProduct_nameUnit_Price
0511/5/20215Keyboard500.000
164/23/20215Mouse200.000
21008/21/202110Monitor5000.235
3Not Available09/18/202120CPU10000.550
4Not Available09/18/202120CPU10000.550
5NaT01/05/20218Speakers250.500
6NaTNaTNaTNaTNaN

Sorting Columns Alphabetically Using Sort_Index()

The Sort_index() method is typically used to sort the index column. Either the row index or the column index.

To sort the column index,

  • Pass axis=1 to denote that the column axes are to be renamed.

Snippet

df = df.sort_index(axis=1)

df

Dataframe Will Look Like

The columns of the dataframe are sorted alphabetically, as shown below.

Available_QuantityAvailable_Since_DateNo_Of_UnitsProduct_nameUnit_Price
0511/5/20215Keyboard500.000
164/23/20215Mouse200.000
21008/21/202110Monitor5000.235
3Not Available09/18/202120CPU10000.550
4Not Available09/18/202120CPU10000.550
5NaT01/05/20218Speakers250.500
6NaTNaTNaTNaTNaN

This is how you can sort columns using the sort_index() alphabetical order.

Sorting Columns in Descending order

You can sort the columns in a descending alphabetical order using the sort_index() method.

To sort columns in descending order,

  • pass axis=1 to denote that column axes must be reordered
  • Pass ascending=False to denote that the columns must be sorted in descending order.

Code

df2 = df.sort_index(axis=1, ascending=False)

df2

Dataframe Will Look Like

Unit_PriceProduct_nameNo_Of_UnitsAvailable_Since_DateAvailable_Quantity
0500.000Keyboard511/5/20215
1200.000Mouse54/23/20216
25000.235Monitor1008/21/202110
310000.550CPU2009/18/2021Not Available
410000.550CPU2009/18/2021Not Available
5250.500Speakers801/05/2021NaT
6NaNNaTNaTNaTNaT

Move Column to First Position

In this section, you’ll learn how to move a column to the first position.

There are no methods directly available to move a column to the first position.

  1. Pop out the column to be moved using the pop() method
  2. Insert the column into the 0th position using the insert() method.

This will move the column to the front position.

Code

column_to_reorder = df.pop('Unit_Price')

df.insert(0, 'Unit_Price', column_to_reorder)

df

Dataframe Will Look Like

By executing the above code, the column Unit_Price Column is shifted to the first position.

Unit_PriceProduct_nameNo_Of_UnitsAvailable_Since_DateAvailable_Quantity
0500.000Keyboard511/5/20215
1200.000Mouse54/23/20216
25000.235Monitor1008/21/202110
310000.550CPU2009/18/2021Not Available
410000.550CPU2009/18/2021Not Available
5250.500Speakers801/05/2021NaT
6NaNNaTNaTNaTNaT

Move Column to Last Position

As similar to moving a column to the first position, there are no methods directly available to move a column to the last position.

  1. Pop out the column to be moved using the pop() method
  2. Find the length of the dataframe column using len(df. columns). Since position is 0 based, you can use the length to insert into the last position.
  3. Insert the column into the len(df. columns) position using the insert() method.

This will move the column to the end position.

Code

column_to_reorder = df.pop('Unit_Price')

df.insert(len(df. columns), 'Unit_Price', column_to_reorder)

df

Dataframe Will Look Like

By executing the above code, the column Unit_Price Column is shifted to the last position.

Product_nameNo_Of_UnitsAvailable_Since_DateAvailable_QuantityUnit_Price
0Keyboard511/5/20215500.000
1Mouse54/23/20216200.000
2Monitor1008/21/2021105000.235
3CPU2009/18/2021Not Available10000.550
4CPU2009/18/2021Not Available10000.550
5Speakers801/05/2021NaT250.500
6NaTNaTNaTNaTNaN

Move Column To Specific Position

You can move the column to a specific position by popping out the column and inserting it into the specific position.

  1. Define the position to which the column needs to be moved
  2. Pop out the column to be moved using the pop() method
  3. Insert the popped up column to the defined position using the insert() method.

Code

position =2

column_to_reorder = df.pop('Unit_Price')

df.insert(position, 'Unit_Price', column_to_reorder)

df

Dataframe Will Look Like

The column Unit_Price Column is shifted to the 3rd position because the index is 0 based while using the insert() method.

Product_nameNo_Of_UnitsUnit_PriceAvailable_Since_DateAvailable_Quantity
0Keyboard5500.00011/5/20215
1Mouse5200.0004/23/20216
2Monitor105000.23508/21/202110
3CPU2010000.55009/18/2021Not Available
4CPU2010000.55009/18/2021Not Available
5Speakers8250.50001/05/2021NaT
6NaTNaTNaNNaTNaT

This is how you can move columns to either first, last, or specified position.

Change Order Like Other DataFrame

Until now, you’ve learned how to reorder the columns in the same dataframe using the same dataframe columns.

To reindex columns of a dataframe similar to the order of the dataframe,

  • Use the method reindex_like() method.
  • It’ll reorder the columns based on the order of the columns in the dataframe passed.

Code

df = df.reindex_like(df2)

df

Dataframe Will Look Like

The columns in the dataframe df will be reordered similar to the order of columns in dataframe df2.

Unit_PriceProduct_nameNo_Of_UnitsAvailable_Since_DateAvailable_Quantity
0500.000Keyboard511/5/20215
1200.000Mouse54/23/20216
25000.235Monitor1008/21/202110
310000.550CPU2009/18/2021Not Available
410000.550CPU2009/18/2021Not Available
5250.500Speakers801/05/2021NaT
6NaNNaTNaTNaTNaT

This is how you can rename the columns of a dataframe based on the order of the columns from the other dataframe.

You May Also Like

Leave a Comment