.copy()
Published Apr 2, 2023
Contribute to Docs
In Pandas, .copy()
is a method that creates a copy of a DataFrame or a Series. This method returns a fully independent copy of DataFrame or a shallow copy (see Shallow vs. Deep Copy section).
Syntax
df_copy = df.copy()
The boolean parameter deep
specifies whether .copy()
should create a deep or a shallow copy. The default value is deep=True
, which enforces a deep copy.
Shallow vs. Deep Copy
Note that the designation of the deep
parameter can have impacts on the resulting copy as well as the original DataFrame.
- In a shallow copy, the new object points to the same data as the original object, and any changes made to the copy will affect the original object. Changes made to the original dataframe will also be reflected in the shallow copy. By default, the
.copy()
method creates a deep copy. - In a deep copy, a new object is created with a completely new set of data that is identical to the original data. Changes made to the copied object will not affect the original object and vice versa.
Example
import pandas as pd# Create a DataFramedf = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})# Create a deep copy of the DataFramedeep_copy = df.copy(deep=True)# Create a shallow copy of the DataFrameshallow_copy = df.copy(deep=False)# Modify the deep copydeep_copy['A'][1] = 20# Modify the shallow copyshallow_copy['A'][0] = 10#Print the deep copyprint(deep_copy)# Print the shallow copyprint(shallow_copy)# Print the original DataFrameprint(df)
The output of the code above will be:
A B0 1 41 20 52 3 6A B0 10 41 2 52 3 6A B0 10 41 2 52 3 6
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.