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_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.
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
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_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 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_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 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_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 Using Reindex()
To sort the columns alphabetically using reindex()
,
- 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 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_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 |
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_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.
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_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 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.
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_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.
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_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.
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_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 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_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.