w3resource

Pandas DataFrame: copy() function

DataFrame - copy() function

The copy() function is used to 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)

Syntax:

copy=True, errors='raise', **kwarg

Parameters:

Name Description Type/Default Value Required / Optional
deep Make a deep copy, including a copy of the data and the indices. With deep=False neither the indices nor the data are copied. bool
Default Value: True
Required
copy  Object type matches caller.  Series or DataFrame Required

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.

Example:


Download the Pandas DataFrame Notebooks from here.

Previous: DataFrame - infer_objects() function
Next: DataFrame - isna() function



Follow us on Facebook and Twitter for latest update.