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…
You can use the below code snippet to change the order of columns of the pandas dataframe.
You can pass the columns list in the order you want. Then the dataframe columns will be reordered.
df = df.reindex(columns=['Available_Since_Date','Product_name', 'Unit_Price','No_Of_Units','Available_Quantity'])
df
When you execute the above code, the column Available_Since_date will be moved to the first position.
Dataframe Will Look Like
Available_Since_Date | Product_name | Unit_Price | No_Of_Units | Available_Quantity | |
---|---|---|---|---|---|
0 | 11/5/2021 | Keyboard | 500.000 | 5 | 5 |
1 | 4/23/2021 | Mouse | 200.000 | 5 | 6 |
2 | 08/21/2021 | Monitor | 5000.235 | 10 | 10 |
3 | 09/18/2021 | CPU | 10000.550 | 20 | Not Available |
4 | 09/18/2021 | CPU | 10000.550 | 20 | Not Available |
5 | 01/05/2021 | Speakers | 250.500 | 8 | NaT |
6 | NaT | NaT | NaN | NaT | NaT |
If You Want to Understand Details, Read on…
In this tutorial, you’ll learn the different methods available to reorder columns in the dataframe and also learn how to sort the columns of the dataframe alphabetically in ascending or descending order.
To sort based on the column values instead, read How to Sort Pandas dataframe Based on Column Value.
Table of Contents
Sample Dataframe
Create a sample dataframe that you’ll use throughout this tutorial.
Column Order
- Product_name
- Unit_Price
- No_Of_Units
- Available_Quantity
- Available_Since_Date
Snippet
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_name | Unit_Price | No_Of_Units | Available_Quantity | Available_Since_Date | |
---|---|---|---|---|---|
0 | Keyboard | 500.000 | 5 | 5 | 11/5/2021 |
1 | Mouse | 200.000 | 5 | 6 | 4/23/2021 |
2 | Monitor | 5000.235 | 10 | 10 | 08/21/2021 |
3 | CPU | 10000.550 | 20 | Not Available | 09/18/2021 |
4 | CPU | 10000.550 | 20 | Not Available | 09/18/2021 |
5 | Speakers | 250.500 | 8 | NaT | 01/05/2021 |
6 | NaT | NaN | NaT | NaT | NaT |
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 list
in the order of how you want to rearrange them.
Snippet
df = df.reindex(columns=['Available_Since_Date','Product_name', 'Unit_Price','No_Of_Units','Available_Quantity'])
df
When you execute the above code, the column Available_Since_Date is moved to the first position. As in the order you’ve specified for the columns attribute.
Dataframe Will Look Like
Available_Since_Date | Product_name | Unit_Price | No_Of_Units | Available_Quantity | |
---|---|---|---|---|---|
0 | 11/5/2021 | Keyboard | 500.000 | 5 | 5 |
1 | 4/23/2021 | Mouse | 200.000 | 5 | 6 |
2 | 08/21/2021 | Monitor | 5000.235 | 10 | 10 |
3 | 09/18/2021 | CPU | 10000.550 | 20 | Not Available |
4 | 09/18/2021 | CPU | 10000.550 | 20 | Not Available |
5 | 01/05/2021 | Speakers | 250.500 | 8 | NaT |
6 | NaT | NaT | NaN | NaT | NaT |
Using List of Columns Names
You can use a list of column names also and pass that list to the reindex()
method as shown below.
Snippet
column_names = ['Available_Since_Date','Product_name', 'Unit_Price','No_Of_Units','Available_Quantity']
df = df.reindex(columns=column_names)
df
Now also the columns are arranged as the order passed in the list.
Dataframe Will Look Like
Available_Since_Date | Product_name | Unit_Price | No_Of_Units | Available_Quantity | |
---|---|---|---|---|---|
0 | 11/5/2021 | Keyboard | 500.000 | 5 | 5 |
1 | 4/23/2021 | Mouse | 200.000 | 5 | 6 |
2 | 08/21/2021 | Monitor | 5000.235 | 10 | 10 |
3 | 09/18/2021 | CPU | 10000.550 | 20 | Not Available |
4 | 09/18/2021 | CPU | 10000.550 | 20 | Not Available |
5 | 01/05/2021 | Speakers | 250.500 | 8 | NaT |
6 | NaT | NaT | NaN | NaT | NaT |
Using Dataframe Indexing
You can use the dataframe indexing using the column names and create a new dataframe out of it to rearrange the columns.
Snippet
df = df[['Available_Since_Date','Product_name', 'Unit_Price','No_Of_Units','Available_Quantity']]
df
When you execute the above snippet, the dataframe columns will be rearranged to the order passed for the dataframe indexing.
Dataframe Will Look Like
Available_Since_Date | Product_name | Unit_Price | No_Of_Units | Available_Quantity | |
---|---|---|---|---|---|
0 | 11/5/2021 | Keyboard | 500.000 | 5 | 5 |
1 | 4/23/2021 | Mouse | 200.000 | 5 | 6 |
2 | 08/21/2021 | Monitor | 5000.235 | 10 | 10 |
3 | 09/18/2021 | CPU | 10000.550 | 20 | Not Available |
4 | 09/18/2021 | CPU | 10000.550 | 20 | Not Available |
5 | 01/05/2021 | Speakers | 250.500 | 8 | NaT |
6 | NaT | NaT | NaN | NaT | NaT |
This is how you can rearrange columns using dataframe indexing.
Sorting Columns Alphabetically
In this section, you’ll learn how to sort columns of the dataframe alphabetically using reindex()
and sort_index()
.
Using Reindex()
You can sort the columns of the dataframe using the reindex()
method and sorted()
method.
- Fetch the existing columns for the dataframe using
df.columns
- Pass it to the
sorted()
method. It’ll sort the methods alphabetically - Pass the sorted value to the
reindex()
method - Specify
axis=1
to denote that the column axes needs to be reordered
df = df.reindex(sorted(df.columns), axis=1)
df
Now the dataframe columns will be sorted alphabetically as below.
Dataframe Will Look Like
Available_Quantity | Available_Since_Date | No_Of_Units | Product_name | Unit_Price | |
---|---|---|---|---|---|
0 | 5 | 11/5/2021 | 5 | Keyboard | 500.000 |
1 | 6 | 4/23/2021 | 5 | Mouse | 200.000 |
2 | 10 | 08/21/2021 | 10 | Monitor | 5000.235 |
3 | Not Available | 09/18/2021 | 20 | CPU | 10000.550 |
4 | Not Available | 09/18/2021 | 20 | CPU | 10000.550 |
5 | NaT | 01/05/2021 | 8 | Speakers | 250.500 |
6 | NaT | NaT | NaT | NaT | NaN |
Using Sort_Index()
You can sort the columns of the dataframe using the sort_index()
method. Sort_index is typically used to sort the index column. Either the row
index or the column
index.
To sort the column index, you need to pass axis=1
to denote that the column axes are to be renamed.
Snippet
df = df.sort_index(axis=1)
df
When you execute the above code, the columns of the dataframe will be sorted alphabetically as shown below.
Dataframe Will Look Like
Available_Quantity | Available_Since_Date | No_Of_Units | Product_name | Unit_Price | |
---|---|---|---|---|---|
0 | 5 | 11/5/2021 | 5 | Keyboard | 500.000 |
1 | 6 | 4/23/2021 | 5 | Mouse | 200.000 |
2 | 10 | 08/21/2021 | 10 | Monitor | 5000.235 |
3 | Not Available | 09/18/2021 | 20 | CPU | 10000.550 |
4 | Not Available | 09/18/2021 | 20 | CPU | 10000.550 |
5 | NaT | 01/05/2021 | 8 | Speakers | 250.500 |
6 | NaT | NaT | NaT | NaT | NaN |
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.
Parameters
axis=1
– To denote that column axes must be reorderedascending=False
– To denote that the columns must be sorted in a descending order.
Snippet
df2 = df.sort_index(axis=1, ascending=False)
df2
When you execute the above code, you’ll see the columns of the dataframe are sorted in descending alphabetical order.
Dataframe Will Look Like
Unit_Price | Product_name | No_Of_Units | Available_Since_Date | Available_Quantity | |
---|---|---|---|---|---|
0 | 500.000 | Keyboard | 5 | 11/5/2021 | 5 |
1 | 200.000 | Mouse | 5 | 4/23/2021 | 6 |
2 | 5000.235 | Monitor | 10 | 08/21/2021 | 10 |
3 | 10000.550 | CPU | 20 | 09/18/2021 | Not Available |
4 | 10000.550 | CPU | 20 | 09/18/2021 | Not Available |
5 | 250.500 | Speakers | 8 | 01/05/2021 | NaT |
6 | NaN | NaT | NaT | NaT | NaT |
This is how you can use reindex()
and sort_index()
methods to change the order of the dataframe.
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.
- Pop out the column to be moved using the
pop()
method - Insert the column into the
0th
position using theinsert()
method.
This will move the column to the front position.
Snippet
column_to_reorder = df.pop('Unit_Price')
df.insert(0, 'Unit_Price', column_to_reorder)
df
By executing the above snippet, the column Unit_Price Column is shifted to the first position.
Dataframe Will Look Like
Unit_Price | Product_name | No_Of_Units | Available_Since_Date | Available_Quantity | |
---|---|---|---|---|---|
0 | 500.000 | Keyboard | 5 | 11/5/2021 | 5 |
1 | 200.000 | Mouse | 5 | 4/23/2021 | 6 |
2 | 5000.235 | Monitor | 10 | 08/21/2021 | 10 |
3 | 10000.550 | CPU | 20 | 09/18/2021 | Not Available |
4 | 10000.550 | CPU | 20 | 09/18/2021 | Not Available |
5 | 250.500 | Speakers | 8 | 01/05/2021 | NaT |
6 | NaN | NaT | NaT | NaT | NaT |
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.
- Pop out the column to be moved using the
pop()
method - 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. - Insert the column into the
len(df. columns)
position using theinsert()
method.
This will move the column to the end position.
Snippet
column_to_reorder = df.pop('Unit_Price')
df.insert(len(df. columns), 'Unit_Price', column_to_reorder)
df
By executing the above snippet, the column Unit_Price Column is shifted to the last position.
Dataframe Will Look Like
Product_name | No_Of_Units | Available_Since_Date | Available_Quantity | Unit_Price | |
---|---|---|---|---|---|
0 | Keyboard | 5 | 11/5/2021 | 5 | 500.000 |
1 | Mouse | 5 | 4/23/2021 | 6 | 200.000 |
2 | Monitor | 10 | 08/21/2021 | 10 | 5000.235 |
3 | CPU | 20 | 09/18/2021 | Not Available | 10000.550 |
4 | CPU | 20 | 09/18/2021 | Not Available | 10000.550 |
5 | Speakers | 8 | 01/05/2021 | NaT | 250.500 |
6 | NaT | NaT | NaT | NaT | NaN |
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.
- Define the position to which the column needs to be moved
- Pop out the column to be moved using the
pop()
method - Insert the popped up column to the defined position using the
insert()
method.
Snippet
position =2
column_to_reorder = df.pop('Unit_Price')
df.insert(position, 'Unit_Price', column_to_reorder)
df
By executing the above snippet, the column Unit_Price Column is shifted to the 3rd
position. Because the index is 0
based while using the insert() method.
Dataframe Will Look Like
Product_name | No_Of_Units | Unit_Price | Available_Since_Date | Available_Quantity | |
---|---|---|---|---|---|
0 | Keyboard | 5 | 500.000 | 11/5/2021 | 5 |
1 | Mouse | 5 | 200.000 | 4/23/2021 | 6 |
2 | Monitor | 10 | 5000.235 | 08/21/2021 | 10 |
3 | CPU | 20 | 10000.550 | 09/18/2021 | Not Available |
4 | CPU | 20 | 10000.550 | 09/18/2021 | Not Available |
5 | Speakers | 8 | 250.500 | 01/05/2021 | NaT |
6 | NaT | NaT | NaN | NaT | NaT |
This is how you can move columns to either first, last, or a 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.
If you want to reindex columns of a dataframe similar to the order of the dataframe, you can use the method reindex_like()
method.
It’ll reorder the columns based on the order of the columns in the dataframe passed.
Snippet
df = df.reindex_like(df2)
df
When executing the above snippet, the columns in the dataframe df
will be reordered similar to the order of columns in dataframe df2
.
Dataframe Will Look Like
Unit_Price | Product_name | No_Of_Units | Available_Since_Date | Available_Quantity | |
---|---|---|---|---|---|
0 | 500.000 | Keyboard | 5 | 11/5/2021 | 5 |
1 | 200.000 | Mouse | 5 | 4/23/2021 | 6 |
2 | 5000.235 | Monitor | 10 | 08/21/2021 | 10 |
3 | 10000.550 | CPU | 20 | 09/18/2021 | Not Available |
4 | 10000.550 | CPU | 20 | 09/18/2021 | Not Available |
5 | 250.500 | Speakers | 8 | 01/05/2021 | NaT |
6 | NaN | NaT | NaT | NaT | NaT |
This is how you can rename the columns of a dataframe based on the order of the columns from the other dataframe.
Conclusion
To summarize, You’ve learned how to change the order of the dataframe columns using the reindex()
method and also using the dataframe indexing.
You’ve also learned to sort the columns alphabetically in ascending or descending order using reindex()
or sort_index()
methods.
Also, you’ve learned how to move the column to the first position, last position, or to a specific position.
All these operations can be used when you want to perform various data manipulation operations in the pandas dataframe.
If you have any questions, comment below.