Introduction
In this article, we will cover how to use the Pandas Copy function to copy DataFrames along with syntax and examples.
Syntax of Pandas Copy Function | pandas.DataFrame.copy
The Pandas Copy function is part of the DataFrame class and follows a simple syntax:
DataFrame.copy(deep=True)
- deep : This parameter specifies whether to create a deep copy or a shallow copy of the DataFrame. By default, it is set to True for a deep copy.
Example – 1: Copy Pandas DataFrame with Deep = False
In this example, we first create a sample Data Frame and copy it to the second Data Frame using the copy() function with Deep = False parameter. Next, we modify the second Data Frame which in turn also modifies the original Data Frame. This is because due to Deep = False parameter, a shallow copy of the second data frame is created that references the original Data Frame only.
In [0]:
import pandas as pd # Creating a DataFrame data = {'Name': ['John', 'Alice', 'Bob'], 'Age': [28, 24, 22]} df_original = pd.DataFrame(data) # Displaying original print(" Original DataFrame Before:\n", df_original) # Creating a shallow copy df_shallow_copy = df_original.copy(deep=False) # Modifying the copied DataFrame df_shallow_copy['Age'][0] = 30 # Displaying original and copied DataFrames print("\n Shallow Copy DataFrame:\n", df_shallow_copy) print("\n Original DataFrame After:\n", df_original)
Out[0]:
Original DataFrame Before: Name Age 0 John 28 1 Alice 24 2 Bob 22 Shallow Copy DataFrame: Name Age 0 John 30 1 Alice 24 2 Bob 22 Original DataFrame After: Name Age 0 John 30 1 Alice 24 2 Bob 22
Example – 2: Copy Pandas DataFrame with Deep = True
In this example, we copy the sample Data Frame to the second Data Frame using the copy() function with Deep = True parameter. But unlike the shallow copy, modifying the deep copy does not affect the original Data Frame. This is because the deep copy creates a completely new copy of the objects that does not reference to original Data Frame.
In [1]:
import pandas as pd # Creating a DataFrame data = {'Name': ['John', 'Alice', 'Bob'], 'Age': [28, 24, 22]} df_original = pd.DataFrame(data) print(" Original DataFrame Before:\n", df_original) # Creating a deep copy df_deep_copy = df_original.copy(deep=True) # Modifying the copied DataFrame df_deep_copy['Age'][0] = 30 # Displaying original and copied DataFrames print("\n Deep Copy DataFrame:\n", df_deep_copy) print("\n Original DataFrame After:\n", df_original)
Out[1]:
Original DataFrame Before: Name Age 0 John 28 1 Alice 24 2 Bob 22 Deep Copy DataFrame: Name Age 0 John 30 1 Alice 24 2 Bob 22 Original DataFrame After: Name Age 0 John 28 1 Alice 24 2 Bob 22
Example – 3: Copy Pandas DataFrame Column To Another DataFrame
In this example, we show how to copy specific Pandas Data Frame columns to another Data Frame. This can be done by specifying the column names of the original Data Frame while invoking the copy() function.
In [2]:
import pandas as pd # Creating a DataFrame data = {'Name': ['John', 'Alice', 'Bob'], 'Age': [28, 24, 22], 'City': ['New York', 'San Francisco', 'Chicago']} df_original = pd.DataFrame(data) # Creating a copy with selected columns df_copy_selected_columns = df_original[['Name', 'Age']].copy() # Displaying original and copied DataFrames print("Original DataFrame:\n", df_original) print("\nCopy with Selected Columns:\n", df_copy_selected_columns)
Out[2]:
Original DataFrame: Name Age City 0 John 28 New York 1 Alice 24 San Francisco 2 Bob 22 Chicago Copy with Selected Columns: Name Age 0 John 28 1 Alice 24 2 Bob 22