pandas.DataFrame.insert

pandas 3.1.1 · DataFrame · method

Insert a column at the specified position in the existing DataFrame. A column with the same name raises ValueError by default.

Call syntax

DataFrame.insert(loc, column, value, allow_duplicates=False)

Run the example

Edit the example and select Run Python to see the result.

Parameters

ParameterRequirementAccepted values and behavior
loc Required
Positional or keyword
int

The zero-based position for the new column. Use 0 for the first column. Use the number of existing columns to append.

column Required
Positional or keyword
str, number, or hashable object

The name of the new column. Use a string, a number, or another hashable label.

value Required
Positional or keyword
Scalar, Series, or array-like

Data for the new column. A scalar is repeated in every row. An array or list must match the row count. A Series aligns by the DataFrame index, leaving missing values for unmatched rows.

allow_duplicates Optional
Positional or keyword
Default False
bool

The default False raises ValueError if the name already exists. Set True to allow the duplicate when the DataFrame permits duplicate labels.

Returns

Returns None and adds the column to the existing DataFrame.