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 theinsert()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 isFalse, it checks whether the inserted column already exists or not. if it is set toTrue, it means duplicate column names are allowed.
Note:IndexErrorthrows index out of bounds error whenlocinteger value does not lie between column ranges.
Return value
It does not return any value and update the caller DataFrame.
Example
# importing pandas moduleimport pandas as pd#importing the random moduleimport random# creating a blank seriesType_new = pd.Series([])# Creating a data frame along with column namedata = [["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 DataFramefor i in range(df["Name"].size):Type_new[i]=random.randint(1,9)# inserting ID as a new featuredf.insert(0, "Id", Type_new)# printing DataFrameprint(df)
Explanation
- Line 6: We create a blank series,
Type_new, to keep randomly generatedIdsin 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
Idsto insert into DataFrame to update employee records. - Line 19: We invoke
df.insert()to insert new feature or column at the index0with randomly generatedIds. - Line 21: We print the updated DataFrame with
Idsas a new column.