Pandas DataFrame insert() Method
Example
Insert a new column with age of each member, and place it between "name" and "qualified":
import pandas as pd
data = {
"name": ["Sally", "Mary",
"John"],
"qualified": [True, False, False]
}
df = pd.DataFrame(data)
df.insert(1, "age", [50, 40, 30])
print(df)
Try it Yourself »
Definition and Usage
The insert()
method allows us to insert a
new column to an existing DataFrame.
Syntax
dataframe.insert(loc, column, value,
allow_duplicates)
Parameters
Parameter | Description |
---|---|
loc | Required. An integer specifying the location of the new column. |
columns | Required. A string specifying the name of the new column. |
value | Required. An array-like object specifying the values of the new column. |
allow_duplicates | Optional. Default False, specifying whether the DataFrame allows columns with the same name or not. |
Return Value
None. The insertion is done in the original DataFrame.
Copyright 1999-2023 by Refsnes Data. All Rights Reserved.