What is the DataFrame.insert() method of Pandas in Python?

Overview

In Python, the DataFrame.Insert() method is used to insert a column in a DataFrame or series.

Note: Column insertion is also possible without using the insert() method, but it is always inserted as the last column.

Syntax


DataFrameName.insert(loc, column, value, allow_duplicates = False)
Syntax of DataFrameName.insert()

Parameters

It takes the following argument values:

  • loc: An integer as an index value or position to insert a new column.
  • column: A string as the name of the column to be inserted.
  • value: Any datatype value, which we're going to insert.
  • allow_douplicates: Its default value is False, it checks whether the inserted column already exists or not. if it is set to True, it means duplicate column names are allowed.
Note: IndexError throws index out of bounds error when loc integer value does not lie between column ranges.

Return value

It does not return any value and update the caller DataFrame.

Example

# importing pandas module
import pandas as pd
#importing the random module
import random
# creating a blank series
Type_new = pd.Series([])
# Creating a data frame along with column name
data = [["Isabella", 23, 8900, 2020],
["Elijah", 34, 6000, 2017],
["Lucas", 45, 6570, 2021],
["Olivia", 55, 13000, 2022],
["William", 32, 13600, 2019]]
df = pd.DataFrame(data, columns=[
'Name', 'Age', 'Salary', 'Join Date'])
# inserting into DataFrame
for i in range(df["Name"].size):
Type_new[i]=random.randint(1,9)
# inserting ID as a new feature
df.insert(0, "Id", Type_new)
# printing DataFrame
print(df)

Explanation

  • Line 6: We create a blank series, Type_new, to keep randomly generated Ids in line 17.
  • Line 8: We create a nested list that contains the employee's name, age, salary and join date.
  • Line 13: We convert the list into a Pandas DataFrame.
  • Line 16: In this loop, we randomly generate Ids to insert into DataFrame to update employee records.
  • Line 19: We invoke df.insert() to insert new feature or column at the index 0 with randomly generated Ids.
  • Line 21: We print the updated DataFrame with Ids as a new column.

Free Resources