Open In App

Insert a given column at a specific position in a Pandas DataFrame

Last Updated : 30 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this comprehensive guide, we will leverage the powerful DataFrame.insert() the method provided by the Pandas library to effectively Insert a given column at a specific position in a Pandas Dataframe.

Create a Sample DataFrame

In this example below code uses Pandas to create a DataFrame named ‘df’ from a dictionary with columns ‘col2’ and ‘col3’ and their respective values. The data frame is then displayed.

Python3




# Importing pandas library
import pandas as pd
 
# dictionary
values = {'col2': [6, 7, 8,
                   9, 10],
          'col3': [11, 12, 13,
                   14, 15]}
 
# Creating dataframe
df = pd.DataFrame(values)
 
# show the dataframe
df


Output:

   col2  col3
0     6    11
1     7    12
2     8    13
3     9    14
4    10    15

Ways to Add a column in Pandas DataFrame

There are various ways to Insert a given column at a specific position in a Pandas Dataframe. In this article, we will explain some commonly used methods.

  • Add Column to Dataframe
  • Insert a column at the beginning
  • Insert a column at specific position
  • Insert a column at the end
  • Using assign() method
  • Using insert() method

Pandas Add Column to Dataframe

Here we are adding a new column in the dataframe and then define a list in the same order in which we want to get the columns.

Python3




# adding a new column
 
df['col1']=0
 
# show the dataframe
df = df[list(('col1','col2','col3'))]
 
# show the dataframe
df


Output:

   col1  col2  col3
0     0     6    11
1     0     7    12
2     0     8    13
3     0     9    14
4     0    10    15

Insert a column at the beginning of the dataframe using insert() Method

In this example, we are using an insert() method to Insert a column at the beginning of the Dataframe.

Python3




# New column to be added
new_col = [1, 2, 3, 4, 5]
 
# Inserting the column at the
# beginning in the DataFrame
df.insert(loc = 0,
          column = 'col1',
          value = new_col)
# show the dataframe
df


Output: 

   col1  col2  col3
0     1     6    11
1     2     7    12
2     3     8    13
3     4     9    14
4     5    10    15

Insert a column at the end in a Dataframe using Loc() Method

In this example, we are using the Pandas loc method, a new column named ‘New_Column’ is added with specified values, and the modified DataFrame is displayed.

Python3




# Using loc method
df.loc[:, 'New_Column'] = [20, 30, 40, 50, 60]
 
# Displaying the modified DataFrame
print(df)


Output:

   col2  col3  New_Column
0     6    11          20
1     7    12          30
2     8    13          40
3     9    14          50
4    10    15          60

Insert a column at the specific position of the dataframe

In this example, the below code inserts a new column (‘New_Column’) into an existing DataFrame (df) at a specified position and displays the resulting modified data frame.

Python3




# Creating a new DataFrame
new_df = pd.DataFrame({'New_Column': [20, 30, 40, 50, 60]})
df = pd.concat([df.iloc[:, :1], new_df, df.iloc[:, 1:]], axis=1)
 
# Displaying the modified DataFrame
print(df)


Output :

   col2  New_Column  col3
0     6          20    11
1     7          30    12
2     8          40    13
3     9          50    14
4    10          60    15

Insert a column in a Dataframe using assign() method

In this example below code employs the Pandas assign method to add a new column named ‘New_Column’ with specified values to the DataFrame ‘df’. The modified data frame is then displayed.

Python3




# Using assign method
df = df.assign(New_Column=[20, 30, 40, 50, 60])
 
# Displaying the modified DataFrame
print(df)


Output :

   col2  col3  New_Column
0     6    11          20
1     7    12          30
2     8    13          40
3     9    14          50
4    10    15          60

Insert a column in a Dataframe using insert() method

In this example below code utilizes the Pandas `insert` method to add a new column named ‘New_Column’ at position 1 in the DataFrame, using values from a Pandas Series. The modified data frame is then displayed.

Python3




# Using insert with a Series
df.insert(1, 'New_Column', pd.Series([20, 30, 40, 50, 60]))
 
# Displaying the modified DataFrame
print(df)


Output :

   col2  New_Column  col3
0     6          20    11
1     7          30    12
2     8          40    13
3     9          50    14
4    10          60    15


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads