Primary
DataFrame.insert() ⚬|Documentation|1st|20251021121234-00-⌔
pandas.DataFrame.insert — pandas 2.3.3 documentation#pandas.DataFrame.insert
DataFrame.insert(loc, column, value, allow_duplicates=<no_default>)Insert column into DataFrame at specified location.
Raises a ValueError if column is already contained in the DataFrame, unless allow_duplicates is set to True.
Parameters:
loc: intInsertion index. Must verify 0 <= loc <= len(columns).
column: str, number, or hashable objectLabel of the inserted column.
value: Scalar, Series, or array-likeContent of the inserted column.
allow_duplicates: bool, optional, default lib.no_defaultAllow duplicate column labels to be created.
See also:
Index.insertInsert new item by index.
Examples:
>>> df = pd.DataFrame({"col1": [1, 2], "col2": [3, 4]}) >>> df col1 col2 0 1 3 1 2 4 >>> df.insert(1, "newcol", [99, 99]) >>> df col1 newcol col2 0 1 99 3 1 2 99 4 >>> df.insert(0, "col1", [100, 100], allow_duplicates=True) >>> df col1 col1 newcol col2 0 100 1 99 3 1 100 2 99 4Notice that pandas uses index alignment in case of value from type Series:
>>> 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 4Printed 2026-06-28.
(echo:: @ ᯤ)
Link to original
Secondary
• • •