pby.html)
  * [ Resampling ](../resampling.html)
  * [ Style ](../style.html)
  * [ Plotting ](../plotting.html)
  * [ Options and settings ](../options.html)
  * [ Extensions ](../extensions.html)
  * [ Testing ](../testing.html)
  * [ Missing values ](../missing_value.html)

  * [ __ ](../../index.html)
  * [ API reference ](../index.html)
  * [ DataFrame ](../frame.html)
  * pandas.DataF... 

#  pandas.DataFrame.copy  #

DataFrame.  copy  (  _ deep  =  True  _ )  [ [source]
](https://github.com/pandas-
dev/pandas/blob/v2.2.2/pandas/core/generic.py#L6662-L6815) #

    

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). Any changes to the data of the original will be reflected in the
shallow copy (and vice versa).

Note

The ` deep=False  ` behaviour as described above will change in pandas 3.0. [
Copy-on-Write
](https://pandas.pydata.org/docs/dev/user_guide/copy_on_write.html) will be
enabled by default, which means that the âshallowâ copy is that is
returned with ` deep=False  ` will still avoid making an eager copy, but
changes to the data of the original will _no_ longer be reflected in the
shallow copy (or vice versa). Instead, it makes use of a lazy (deferred) copy
mechanism that will copy the data only when any changes to the original or
shallow copy is made.

You can already get the future behavior and improvements through enabling copy
on write ` pd.options.mode.copy_on_write  =  True  `

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.

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
](../../user_guide/gotchas.html#gotchas-thread-safety) when copying in a
threading environment.

When ` copy_on_write  ` in pandas config is set to ` True  ` , the `
copy_on_write  ` config takes effect even when ` deep=False  ` . 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
](../../user_guide/copy_on_write.html#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()
    >>> s_copy
    a    1
    b    2
    dtype: int64
    

**Shallow copy versus default (deep) copy:**

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

Shallow copy shares data and index with original.

    
    
    >>> s is shallow
    False
    >>> s.values is shallow.values and s.index is shallow.index
    True
    

Deep copy has own copy of data and index.

    
    
    >>> s is deep
    False
    >>> s.values is deep.values or s.index is deep.index
    False
    

Updates to the data shared by shallow copy and original is reflected in both
(NOTE: this will no longer be true for pandas >= 3.0); deep copy re