• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / Pandas 101 / How to Change the Order of Columns in a Pandas Dataframe

How to Change the Order of Columns in a Pandas Dataframe

October 13, 2021 by cmdlinetips

In this tutorial, we will learn how to change the order of columns in Pandas dataframe. We can change the order of the columns in multiple. Here, we will see two ways to change the order of the columns.
How to Change Order of Columns in Pandas
How to Change Column Order in Pandas

First, let us load Pandas.
import pandas as pd

We will use gapminder dataset to change the column orders. Let us load gapminder dataset directly from cmdlinetips.com’s github page.

data_link = "https://raw.githubusercontent.com/cmdlinetips/data/master/gapminder-FiveYearData.csv"
df = pd.read_csv(data_link)

Our data looks like this.

df.head()


country	year	pop	continent	lifeExp	gdpPercap
0	Afghanistan	1952	8425333.0	Asia	28.801	779.445314
1	Afghanistan	1957	9240934.0	Asia	30.332	820.853030
2	Afghanistan	1962	10267083.0	Asia	31.997	853.100710
3	Afghanistan	1967	11537966.0	Asia	34.020	836.197138
4	Afghanistan	1972	13079460.0	Asia	36.088	739.981106

In this dataframe the columns are ordered as

df.columns.tolist()
['country', 'year', 'pop', 'continent', 'lifeExp', 'gdpPercap']

Let us say we want to change the column orders in a specific way. Here is an example order that we want.

col_order = ["continent","country", "lifeExp","pop","gdpPercap"]

Change Order of Columns in a Dataframe

One of the ways to change the column orders is to use the list containing the new column order and subset the dataframe as shown below.

df_new = df[col_order]

The resulting dataframe will contain the columns in the order we want.

df_new.head()

	continent	country	lifeExp	pop	gdpPercap
0	Asia	Afghanistan	28.801	8425333.0	779.445314
1	Asia	Afghanistan	30.332	9240934.0	820.853030
2	Asia	Afghanistan	31.997	10267083.0	853.100710
3	Asia	Afghanistan	34.020	11537966.0	836.197138
4	Asia	Afghanistan	36.088	13079460.0	739.981106

Change Column Orders with Pandas reindex()

Another way to change the order of columns of a Pandas dataframe is to use Pandas reindex() function and use the list with the new column order we need.

Here we need to specify that we are re-indexing the columns, by using the argument “columns”.

df_new = df.reindex(columns=col_order)
df_new.head()

continent	country	lifeExp	pop	gdpPercap
0	Asia	Afghanistan	28.801	8425333.0	779.445314
1	Asia	Afghanistan	30.332	9240934.0	820.853030
2	Asia	Afghanistan	31.997	10267083.0	853.100710
3	Asia	Afghanistan	34.020	11537966.0	836.197138
4	Asia	Afghanistan	36.088	13079460.0	739.981106

If you are interested changing just one of the column’s position. check out Pandas insert() function to move a column to a specific location.

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

Default ThumbnailHow To Get Data Types of Columns in Pandas Dataframe? How To Select Columns in Python Pandas?How To Select One or More Columns in Pandas? Change Column Names and Row Indexes in PandasHow To Change Column Names and Row Indexes in Pandas? Default ThumbnailHow To Select Columns by Data Type in Pandas?

Filed Under: Pandas 101, Python, Python Tips Tagged With: Change Column Orders Pandas, Pandas reindex

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version