During the data preprocessing and feature creation stage, it might happen that you end up with columns that may not necessarily be in the order that you’d like. In this tutorial, we’ll look at how to change the order of columns of a pandas dataframe.
How to reorder columns of a pandas dataframe?
To change the order of columns of a dataframe, you can pass a list with columns in the desired order to []
(that is, indexing with []
). The following is the syntax:
df_correct_order = df[[col1, col2, col3, ..., coln]]
Generally, we use []
in Pandas dataframes to subset a dataframe but it can also be used to reorder the columns. You can also use .loc
and .iloc
to change the order of columns of a dataframe.
Examples
First, let’s create a dataframe that we’ll be using throughout this tutorial.
import pandas as pd data = { 'Name': ['Microsoft Corporation', 'Google, LLC', 'Tesla, Inc.',\ 'Apple Inc.', 'Netflix, Inc.'], 'Shares': [100, 50, 150, 200, 80], 'Symbol': ['MSFT', 'GOOG', 'TSLA', 'AAPL', 'NFLX'] } # create dataframe df = pd.DataFrame(data) # display the dataframe df
Output:
Here, df
is a dataframe of a sample stock portfolio with columns Name
, Shares
, Symbol
. We want to reorder the columns such that the resulting dataframe has columns in the order Name
, Symbol
, Shares
. Let’s see examples of some of the ways we can achieve this.
1. Change column order using []
As mentioned above, you can pass the columns in the order you like as a list.
# new dataframe with different column order df_new = df[['Name', 'Symbol', 'Shares']] # display the dataframe df_new
Output:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
In the above example, we change the order of columns from Name
, Shares
, Symbol
to Name
, Symbol
, Shares
.
2. Change column order using .loc
You can also reorder a pandas dataframe by indexing it using .loc. This way, you can reorder columns using their names as we did in the previous example.
# new dataframe with different column order df_new = df.loc[:, ['Name', 'Symbol', 'Shares']] # display the dataframe df_new
Output:
In the above example, we change the order of columns from Name
, Shares
, Symbol
in the original dataframe df
to Name
, Symbol
, Shares
in the returned dataframe df_new
using the dataframe’s .loc
property.
3. Change column order using .iloc
You can also change the column order of a dataframe by indexing it using .iloc
. Here, we pass the column indexes instead of their names in the order that we want.
# new dataframe with different column order df_new = df.iloc[:, [0, 2, 1]] # display the dataframe df_new
Output:
In the above example, we reorder columns from the original dataframe df
using their indexes rather than their names. The resulting dataframe is saved as df_new
. The columns in the dataframe df_new
are ordered Name
, Symbol
, Shares
.
For more, refer to pandas’ official guide on indexing and selecting data.
With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having pandas version 1.0.5
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.
More on Pandas DataFrames –
- Pandas – Sort a DataFrame
- Change Order of Columns of a Pandas DataFrame
- Pandas DataFrame to a List in Python
- Pandas – Count of Unique Values in Each Column
- Pandas – Replace Values in a DataFrame
- Pandas – Filter DataFrame for multiple conditions
- Pandas – Random Sample of Rows
- Pandas – Random Sample of Columns
- Save Pandas DataFrame to a CSV file
- Pandas – Save DataFrame to an Excel file
- Create a Pandas DataFrame from Dictionary
- Convert Pandas DataFrame to a Dictionary
- Drop Duplicates from a Pandas DataFrame
- Concat DataFrames in Pandas
- Append Rows to a Pandas DataFrame
- Compare Two DataFrames for Equality in Pandas
- Get Column Names as List in Pandas DataFrame
- Select One or More Columns in Pandas
- Pandas – Rename Column Names
- Pandas – Drop one or more Columns from a Dataframe
- Pandas – Iterate over Rows of a Dataframe
- How to Reset Index of a Pandas DataFrame?
- Read CSV files using Pandas – With Examples
- Apply a Function to a Pandas DataFrame