• 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 Move a Column to First Position in Pandas DataFrame?

How To Move a Column to First Position in Pandas DataFrame?

March 27, 2020 by cmdlinetips

In this post, we will learn how to move a single column in a Pandas Dataframe to the first position in Pandas Dataframe. The basic idea is to remove the column/variable from the dataframe using Pandas pop() function and using Pandas insert() function to put it in the first position of Pandas dataframe.
How To Move A Column To Front in Pandas Dataframe?
How To Move A Column To Front in Pandas Dataframe?

Let us load pandas and numpy.
import pandas as pd
import numpy as np
pd.__version__

We will generate some data using NumPy’s random module and store it in a Pandas dataframe. Unlike before, here we create a Pandas dataframe using two-dimensional NumPy array of size 8×3 and specify column names for the dataframe with the argument “columns”.

np.random.seed(42)
df =pd.DataFrame(np.random.randint(20, size=(8,3)),
                   columns=list('BCA'))

Let us get a look at the top 5 rows of the dataframe with Pandas head() function.

df.head()
	B	C	A
0	6	19	14
1	10	7	6
2	18	10	10
3	3	7	2
4	1	11	5

We can see that columns in the data frame are out of order and we would like to move the third column A to the first position of the dataframe.

To move a column to first column in Pandas dataframe, we first use Pandas pop() function and remove the column from the data frame.

Here we remove column “A” from the dataframe and save it in a variable.

col_name="A"
first_col = df.pop(col_name)
first_col
0    14
1     6
2    10
3     2
4     5
5    11
6     9
7    14
Name: A, dtype: int64

Now original datafram does not contain the variable that we wanted to move to the first column.

df.head()
	B	C
0	6	19
1	10	7
2	18	10
3	3	7
4	1	11

Now we can use Pandas insert() function and insert the opped column into first position of the dataframe. The first argument of insert() function is the location we want to insert, here it is 0.

df.insert(0, col_name, first_col)

Now we have moved the column of interest to first column in Pandas data frame.

df.head()

	A	B	C
0	14	6	19
1	6	10	7
2	10	18	10
3	2	3	7
4	5	1	11

Share this:

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

Related posts:

How To Split A Column or Column Names in Pandas and Get Part of it? Default ThumbnailHow to Filter Rows Based on Column Values with query function in Pandas? Default ThumbnailHow To Insert a Column at Specific Location in Pandas DataFrame? Default ThumbnailHow to Get Frequency Counts of a Column in Pandas Dataframe: Pandas Tutorial

Filed Under: Pandas 101

Reader Interactions

Trackbacks

  1. How To Insert a Column at Specific Location in Pandas DataFrame? - Python and R Tips says:
    April 1, 2020 at 6:09 am

    […] a column is to use two dataframe with common ID and merge them. One of the common applications of Pandas insert() function is to move a column to the front of the […]

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