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.
DataFrameName.insert(loc, column, value, allow_duplicates = False)
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 whenloc
integer value does not lie between column ranges.
It does not return any value and update the caller DataFrame.
# 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)
Type_new
, to keep randomly generated Ids
in line 17.Ids
to insert into DataFrame to update employee records. df.insert()
to insert new feature or column at the index 0
with randomly generated Ids
.Ids
as a new column.