Pandas DataFrame.insert() Function

Minahil Noor Jan 30, 2023
  1. Syntax of pandas.DataFrame.insert():
  2. Example Codes: DataFrame.insert() Method to Insert a Column at the Beginning
  3. Example Codes: DataFrame.insert() Method to Insert a Column at the End
  4. Example Codes: DataFrame.insert() Method to Insert a Duplicate Column
Pandas DataFrame.insert() Function

Python Pandas DataFrame.insert() function inserts a column at a specified location into a DataFrame.

Syntax of pandas.DataFrame.insert():

DataFrame.insert(loc, column, value, allow_duplicates=False)

Parameters

loc It is an integer parameter. It specifies the location of the new column. It must be greater than or equal to 0 and less than or equal to the number of columns.
column It is a string, integer, or an object. It is the label of the new column.
value It is an integer, series, or array-like parameter. It shows the values of the new column.
allow_duplicates It is a Boolean parameter. It specifies whether the two columns can be the same or not.

Return

It makes changes to the original Dataframe and adds a new column.

Example Codes: DataFrame.insert() Method to Insert a Column at the Beginning

import pandas as pd

dataframe=pd.DataFrame({'Attendance': {0: 60, 1: 100, 2: 80,3: 78,4: 95},
                        'Name': {0: 'Olivia', 1: 'John', 2: 'Laura',3: 'Ben',4: 'Kevin'},
                        'Obtained Marks': {0: 90, 1: 75, 2: 82, 3: 64, 4: 45}})
print("The Original Data frame is: \n")
print(dataframe)

dataframe.insert(0, "Performance", ["Good", "Bad", "Better", "Better", "Best"])
print("The Modified Data frame is: \n")
print(dataframe)

Output:

The Original Data frame is: 

   Attendance    Name  Obtained Marks
0          60  Olivia              90
1         100    John              75
2          80   Laura              82
3          78     Ben              64
4          95   Kevin              45
The Modified Data frame is: 

  Performance  Attendance    Name  Obtained Marks
0        Good          60  Olivia              90
1         Bad         100    John              75
2      Better          80   Laura              82
3      Better          78     Ben              64
4        Best          95   Kevin              45

The function has added a new column at the beginning.

Example Codes: DataFrame.insert() Method to Insert a Column at the End

import pandas as pd

dataframe=pd.DataFrame({'Attendance': {0: 60, 1: 100, 2: 80,3: 78,4: 95},
                        'Name': {0: 'Olivia', 1: 'John', 2: 'Laura',3: 'Ben',4: 'Kevin'},
                        'Obtained Marks': {0: 90, 1: 75, 2: 82, 3: 64, 4: 45}})
print("The Original Data frame is: \n")
print(dataframe)

dataframe.insert(3, "Performance", ["Good", "Bad", "Better", "Better", "Best"])
print("The Modified Data frame is: \n")
print(dataframe)

Output:

The Original Data frame is: 

   Attendance    Name  Obtained Marks
0          60  Olivia              90
1         100    John              75
2          80   Laura              82
3          78     Ben              64
4          95   Kevin              45
The Modified Data frame is: 

   Attendance    Name  Obtained Marks Performance
0          60  Olivia              90        Good
1         100    John              75         Bad
2          80   Laura              82      Better
3          78     Ben              64      Better
4          95   Kevin              45        Best

The function has added a new column at the end.

Example Codes: DataFrame.insert() Method to Insert a Duplicate Column

import pandas as pd

dataframe=pd.DataFrame({'Attendance': {0: 60, 1: 100, 2: 80,3: 78,4: 95},
                        'Name': {0: 'Olivia', 1: 'John', 2: 'Laura',3: 'Ben',4: 'Kevin'},
                        'Obtained Marks': {0: 90, 1: 75, 2: 82, 3: 64, 4: 45}})
print("The Original Data frame is: \n")
print(dataframe)

dataframe.insert(0, "Attendance", [60, 100, 80, 78, 95], allow_duplicates= True)
print("The Modified Data frame is: \n")
print(dataframe)

Output:

The Original Data frame is: 

   Attendance    Name  Obtained Marks
0          60  Olivia              90
1         100    John              75
2          80   Laura              82
3          78     Ben              64
4          95   Kevin              45
The Modified Data frame is: 

   Attendance  Attendance    Name  Obtained Marks
0          60          60  Olivia              90
1         100         100    John              75
2          80          80   Laura              82
3          78          78     Ben              64
4          95          95   Kevin              45

Now the DataFrame has two columns with the label Attendance.

Related Article - Pandas DataFrame