How to create copy of a DataFrame in Python?

Ways to create a copy of a DataFrame in Python

We use the copy() to create a copy of a pandas DataFrame in Python. It can create two types of copies, a shallow copy or a deep copy.

A shallow copy acts like a reference to the original DataFrame in Python, which means any changes made to this copy are reflected in the original copy.

A deep copy, however, creates a new copy of the DataFrame. Any changes made to this object are not reflected in the original DataFrame.

To create copy of a DataFrame in Python:

  1. Create a DataFrame in Python.
  2. Use the copy() function to create a copy of a DataFrame in Python.

See the code below.

# Create copy of a DataFrame in Python
import pandas as pd 
# Create a DataFrame
dt = pd.DataFrame([[15,26,33,82],[22,92,65,14]], columns = ['c1','c2','c3','c4'])
# Use the copy() function to create copy of a DataFrame in Python
dataframe_copy  = dt.copy(deep = True)
print(dataframe_copy)
# Output:    c1  c2  c3  c4
#          0  15  26  33  82
#          1  22  92  65  14     

In the above example, the dateframe_copy is the deep copy of the original DataFrame dt.

To create a deep copy, we set the deep parameter to True. Otherwise, a shallow copy is created.

As discussed earlier, a shallow copy is the same as a reference to the original DataFrame. Another way to create a reference is by using the assignment operator (=).

To conclude

This particular tutorial demonstrates how to create a copy of a DataFrame in Python. For this, we use the copy() function in Python. We also discussed the two types of copies. A deep copy is an actual copy of a DataFrame. A shallow copy is just a reference to the original object.

Explore more from this category at Python DataFrame. Alternatively, search and view other topics at All Tutorials.