Skip to main content
`Ctrl`+`K`

# pandas.DataFrame.copy

DataFrame.copy(deep=True)

  Make a copy of this object’s indices and data.

  When `deep=True` (default), a new object will be created with a copy of the calling object’s data and indices. Modifications to the data or indices of the copy will not be reflected in the original object (see notes below).

  When `deep=False`, a new object will be created without copying the calling object’s data or index (only references to the data and index are copied). With Copy-on-Write, changes to the original will not be reflected in the shallow copy (and vice versa). The shallow copy uses a lazy (deferred) copy mechanism that copies the data only when any changes to the original or shallow copy are made, ensuring memory efficiency while maintaining data integrity.

  Note

  In pandas versions prior to 3.0, the default behavior without Copy-on-Write was different: changes to the original were reflected in the shallow copy (and vice versa). See the Copy-on-Write user guide for more information.

  Parameters:
    deep bool, default True

      Make a deep copy, including a copy of the data and the indices. With `deep=False` neither the indices nor the data are copied.

  Returns:
    Series or DataFrame

      Object type matches caller.

  See also

  `copy.copy`

    Return a shallow copy of an object.

  `copy.deepcopy`

    Return a deep copy of an object.

  Notes

  When `deep=True`, data is copied but actual Python objects will not be copied recursively, only the reference to the object. This is in contrast to `copy.deepcopy()` in the Standard Library, which recursively copies object data (see examples below).

  While `Index` objects are copied when `deep=True`, the underlying numpy array is not copied for performance reasons. Since `Index` is immutable, the underlying data can be safely shared and a copy is not needed.

  Since pandas is not thread safe, see the gotchas when copying in a threading environment.

  Copy-on-Write protects shallow copies against accidental modifications. This means that any changes to the copied data would make a new copy of the data upon write (and vice versa). Changes made to either the original or copied variable would not be reflected in the counterpart. See Copy_on_Write for more information.

  Examples

      >>> s = pd.Series([1, 2], index=["a", "b"])
      >>> s
      a    1
      b    2
      dtype: int64

      >>> s_copy = s.copy(deep=True)
      >>> s_copy
      a    1
      b    2
      dtype: int64

  Due to Copy-on-Write, shallow copies still protect data modifications. Note shallow does not get modified below.

      >>> s = pd.Series([1, 2], index=["a", "b"])
      >>> shallow = s.copy(deep=False)
      >>> s.iloc[1] = 200
      >>> shallow
      a    1
      b    2
      dtype: int64

  When the data has object dtype, even a deep copy does not copy the underlying Python objects. Updating a nested data object will be reflected in the deep copy.

      >>> s = pd.Series([[1, 2], [3, 4]])
      >>> deep = s.copy()
      >>> s[0][0] = 10
      >>> s
      0    [10, 2]
      1     [3, 4]
      dtype: object
      >>> deep
      0    [10, 2]
      1     [3, 4]
      dtype: object