Pandas DataFrame | insert method
Start your free 7-days trial now!
Pandas DataFrame.insert(~)
method inserts a new column into the source DataFrame.
The insertion is done in-place, that is, the source DataFrame is directly modified and no new DataFrame is created.
Parameters
1. loc
link | int
The integer index of the location to insert.
2. column
link | string
The label of the column you want to insert into.
3. value
link | int
or array-like
The column data. If a single scalar is given, then the value will be repeated to fit the number of columns in the source DataFrame.
4. allow_duplicates
link | boolean
| optional
Whether or not to allow columns with duplicate column labels. If False
, then an error will be raised whenever duplicate column labels are inserted. By default, allow_duplicates=False
.
Return Value
None as the insertion is performed in-place.
Examples
Consider the following DataFrame:
df = pd.DataFrame({"A":[4,5],"B":[6,7]})df
A B0 4 61 5 7
Inserting using a list
To insert a new column C
:
df.insert(2, "C", [8,9])df
A B C0 4 6 81 5 7 9
Inserting using a scalar
To insert a new column C
with a repeated constant:
df.insert(2, "C", 8)df
A B C0 4 6 81 5 7 8
Notice how the value 8
, which is a scalar, has been repeated to fill up the column.
Allowing duplicate column labels
By default, when you try to add a column with a column label that already exists in the source DataFrame, an error will be raised:
df.insert(2, "B", 8)df
ValueError: cannot insert B, already exists
You can still allow for duplicate column labels by setting allow_duplicates=True
:
df.insert(2, "B", 8, allow_duplicates=True)df
A B B0 4 6 81 5 7 8