MCPcopy
hub / github.com/pandas-dev/pandas / insert

Method insert

pandas/core/frame.py:5572–5649  ·  view source on GitHub ↗

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 : int Insertion index. Must verify 0 <= loc <=

(
        self,
        loc: int,
        column: Hashable,
        value: object,
        allow_duplicates: bool | lib.NoDefault = lib.no_default,
    )

Source from the content-addressed store, hash-verified

5570 return self._mgr._get_data_subset_indices(predicate)
5571
5572 def insert(
5573 self,
5574 loc: int,
5575 column: Hashable,
5576 value: object,
5577 allow_duplicates: bool | lib.NoDefault = lib.no_default,
5578 ) -> None:
5579 """
5580 Insert column into DataFrame at specified location.
5581
5582 Raises a ValueError if `column` is already contained in the DataFrame,
5583 unless `allow_duplicates` is set to True.
5584
5585 Parameters
5586 ----------
5587 loc : int
5588 Insertion index. Must verify 0 <= loc <= len(columns).
5589 column : str, number, or hashable object
5590 Label of the inserted column.
5591 value : Scalar, Series, or array-like
5592 Content of the inserted column.
5593 allow_duplicates : bool, optional, default lib.no_default
5594 Allow duplicate column labels to be created.
5595
5596 See Also
5597 --------
5598 Index.insert : Insert new item by index.
5599
5600 Examples
5601 --------
5602 >>> df = pd.DataFrame({"col1": [1, 2], "col2": [3, 4]})
5603 >>> df
5604 col1 col2
5605 0 1 3
5606 1 2 4
5607 >>> df.insert(1, "newcol", [99, 99])
5608 >>> df
5609 col1 newcol col2
5610 0 1 99 3
5611 1 2 99 4
5612 >>> df.insert(0, "col1", [100, 100], allow_duplicates=True)
5613 >>> df
5614 col1 col1 newcol col2
5615 0 100 1 99 3
5616 1 100 2 99 4
5617
5618 Notice that pandas uses index alignment in case of `value` from type `Series`:
5619
5620 >>> df.insert(0, "col0", pd.Series([5, 6], index=[1, 2]))
5621 >>> df
5622 col0 col1 col1 newcol col2
5623 0 NaN 100 1 99 3
5624 1 5.0 100 2 99 4
5625 """
5626 if allow_duplicates is lib.no_default:
5627 allow_duplicates = False
5628 if allow_duplicates and not self.flags.allows_duplicate_labels:
5629 raise ValueError(

Calls 1

_sanitize_columnMethod · 0.95