This tutorial shows how to reorder columns in dataframe using Pandas in Python. This tutorial includes 3 examples using the methods of reindex()
, double brackets [[]]
, and pop()
.
Sample Dataframe
The following is the sample dataframe in Python, which will be later in all examples in this tutorial.
import pandas as pd
# Create a dataframe
car_data = {'Brand': ['Tesla', 'Tesla','Tesla','Ford','Ford','Ford','Toyota','Toyota'],
'Location': ['CA', 'CA','NY','MA','CA','WA','CA','TX'],
'Year':['2019','2018','2020','2019','2016','2010','2018','2021']}
car_data=pd.DataFrame(data=car_data)
#print out the dataframe
print(car_data)
The following is the final sample dataframe.
Brand Location Year 0 Tesla CA 2019 1 Tesla CA 2018 2 Tesla NY 2020 3 Ford MA 2019 4 Ford CA 2016 5 Ford WA 2010 6 Toyota CA 2018 7 Toyota TX 2021
Method 1: Use reindex()
to reorder columns in dataframe
The basic code structure is as follows.
new_colomn_order=["col3", "col1", "col2"] df.reindex(columns=new_colomn_order)
# new column order
new_colomn_order=["Location", "Brand", "Year"]
# reoder the columns in the dataframe
car_data=car_data.reindex(columns=new_colomn_order)
# print out data of new order
print(car_data)
The following is the output. As we can see, the column order has been changed to the new one.
Location Brand Year 0 CA Tesla 2019 1 CA Tesla 2018 2 NY Tesla 2020 3 MA Ford 2019 4 CA Ford 2016 5 WA Ford 2010 6 CA Toyota 2018 7 TX Toyota 2021
Method 2: Use brackets to reorder columns in dataframe
The basic code structure is as follows.
df[["col1", "col2", "col3"]]
The following is the data and reorder example.
# reoder the columns in the dataframe
car_data = car_data[["Location", "Brand", "Year"]]
# print out data of new order
print(car_data)
The following is the output with the updated dataframe.
Location Brand Year 0 CA Tesla 2019 1 CA Tesla 2018 2 NY Tesla 2020 3 MA Ford 2019 4 CA Ford 2016 5 WA Ford 2010 6 CA Toyota 2018 7 TX Toyota 2021
Method 3: Use pop()
to reorder columns in dataframe
We sometimes might only want to move one column and do not want to write out all the column nanmes. We can then use pop()
function to move one column and then use insert()
function to put the column back to the dataframe.
# Use pop() to remove the "Brand" column first
Brand_column=car_data.pop("Brand")
#print out the data after removing Brand column
print(car_data)
#insert the Brand_column back to car_data dataframe
car_data.insert(2,"Brand",Brand_column)
# print out data of new column order
print(car_data)
Location Year 0 CA 2019 1 CA 2018 2 NY 2020 3 MA 2019 4 CA 2016 5 WA 2010 6 CA 2018 7 TX 2021 Location Year Brand 0 CA 2019 Tesla 1 CA 2018 Tesla 2 NY 2020 Tesla 3 MA 2019 Ford 4 CA 2016 Ford 5 WA 2010 Ford 6 CA 2018 Toyota 7 TX 2021 Toyota
For more information regarding how to select rows and columns, you can refer to my other tutorial. You can also refer to my tutorial showing how to modify values in Python