pandas 1.4.2

Parameters
insert(self, loc: 'int', column: 'Hashable', value: 'Scalar | AnyArrayLike', allow_duplicates: 'bool' = False) -> 'None'

Raises a ValueError if :None:None:`column` is already contained in the DataFrame, unless :None:None:`allow_duplicates` is set to True.

Parameters

loc : int

Insertion index. Must verify 0 <= loc <= len(columns).

column : str, number, or hashable object

Label of the inserted column.

value : Scalar, Series, or array-like
allow_duplicates : bool, optional default False

Insert column into DataFrame at specified location.

See Also

Index.insert

Insert new item by index.

Examples

This example is valid syntax, but we were not able to check execution
>>> df = pd.DataFrame({'col1': [1, 2], 'col2': [3, 4]})
... df col1 col2 0 1 3 1 2 4
This example is valid syntax, but we were not able to check execution
>>> df.insert(1, "newcol", [99, 99])
... df col1 newcol col2 0 1 99 3 1 2 99 4
This example is valid syntax, but we were not able to check execution
>>> df.insert(0, "col1", [100, 100], allow_duplicates=True)
... df col1 col1 newcol col2 0 100 1 99 3 1 100 2 99 4

Notice that pandas uses index alignment in case of :None:None:`value` from type Series :

This example is valid syntax, but we were not able to check execution
>>> df.insert(0, "col0", pd.Series([5, 6], index=[1, 2]))
... df col0 col1 col1 newcol col2 0 NaN 100 1 99 3 1 5.0 100 2 99 4
See :

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


File: /pandas/core/frame.py#4381
type: <class 'function'>
Commit: