How to move a column to the first position in a Python DataFrame?

In today’s Data Wrangling tutorial, we would like to show how to quickly modify/ reshuffle the column order in a Python Pandas DataFrame. Specifically we will look into moving a specific column to the DataFrame first position.

Move Pandas column positions

Creating our data

We’ll get started by creating some test data which we’ll use to exemplify the different use cases we’ll be discussing in the tutorial.

import numpy as np
import pandas as pd
np.random.seed(100)

#Define the DataFrame
rand_df = pd.DataFrame(data = np.random.normal(80,10,12).reshape(3,4).round(2), columns = [1,2,3,4])

Here’s the output (your output will be different as these are random numbers):

1234
067.4183.2967.9075.59
196.8964.0878.9098.24
287.1062.5359.8471.63

1. Rearrange column to first position

First we’ll extract the column into a Pandas Series, which we’ll later insert into the first position of our DF. We’ll use the pop() DataFrame method.

last_col = rand_df.pop(4)

As you can see, the column was extracted into a Series object:

type(last_col)
pandas.core.series.Series

Here’s how our DataFrame looks now -note that it has three columns except the index (and not four as shown above).

123
062.5083.4391.53
189.8185.1482.21
278.1182.5575.42

Now let’s go ahead and insert in column index=0 (first position):

# the loc parameter sets the column index position
rand_df.insert(loc= 0 , column= 4, value= last_col)

Let’s see what we just got:

4123
077.4862.5083.4391.53
169.3089.8185.1482.21
284.3578.1182.5575.4

2. Move Pandas column to a specific position (in this case the second)

All we need to do is to set the loc parameter correctly:

rand_df.insert(loc= 1 , column= 4, value= last_col)

3. Reorder columns by index

An alternative solution, would be to re-index the DataFrame:

new_rand_df = pd.DataFrame.reindex(rand_df,columns = [3,2,1,4])
new_rand_df

The output looks as following:

3214
091.5383.4362.5077.48
182.2185.1489.8169.30
275.4282.5578.1184.35

4. Sort columns alphabetically

Sorting the DataFrame columns index will do the trick 🙂

new_rand_df = pd.DataFrame.reindex(rand_df,columns = rand_df.columns.sort_values())

You can learn more about DataFrame sorting in this tutorial.