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).


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.


import pandas as pd
# Create a DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
# Create a deep copy of the DataFrame
deep_copy = df.copy(deep=True)
# Create a shallow copy of the DataFrame
shallow_copy = df.copy(deep=False)
# Modify the deep copy
deep_copy['A'][1] = 20
# Modify the shallow copy
shallow_copy['A'][0] = 10
#Print the deep copy
# Print the shallow copy
# Print the original DataFrame

The output of the code above will be:

0 1 4
1 20 5
2 3 6
0 10 4
1 2 5
2 3 6
0 10 4
1 2 5
2 3 6

All contributors

Looking to contribute?

Learn Python:Pandas on Codecademy