Methods to copy DataFrame in Pandas

In this post, we are going to understand different Methods to copy DataFrame in Pandas with code examples. The Python pandas dataframe.copy() built-in method is used to copy a dataframe.

Pandas dataframe copy() Method


The pandas dataframe.copy() method returns a copy of the dataframe. By default, it returns a ‘deep copy’ which means the change made in the original copy does not reflect in the copy.

Syntax

DataFrame.copy(deep=True)

Parameters

  • deep : It define whether to make deep or shallow copy.
    • True : A new object is created with data and indices.It is default value of deep paramter.Any change made in original copy does not reflect in copied object.
    • False : A new object is created that only reference the data and index that need to copy.Any change made in original copy will reflect copy object.

Return Value:

It returns a series or dataframe

1. How to Create a Deep copy of dataframe


To create a deep copy of the dataframe we can use the dataframe.copy() or dataframe.copy(deep = true).In this example, we are creating a deep copy of the dataframe.

Program Example

import pandas as pd
 
Student_dict = {
    'Name': ['Jack', 'Rack', 'Max', 'David'],
    'Marks':[100,100, 100,100],
    'Subject': ['Math', 'Math', 'Music', 'Physic']
}
 
df = pd.DataFrame(Student_dict)

print('origanl dataframe :\n',df)

#copy dataframe
copieddf = df.copy()

print('\n copies dataframe deep copy:\n',copieddf)
 

Output

origanl dataframe :
     Name  Marks Subject
0   Jack    100    Math
1   Rack    100    Math
2    Max    100   Music
3  David    100  Physic

 copies dataframe deep copy:
     Name  Marks Subject
0   Jack    100    Math
1   Rack    100    Math
2    Max    100   Music
3  David    100  Physic

2. How to Create a Shallow Copy of dataframe


To create a shallow copy of the dataframe we can use the dataframe.copy() or dataframe.copy(deep = False).In this example, we are creating a shallow copy of the dataframe.

Program Example

import pandas as pd
 
Student_dict = {
    'Name': ['Jack', 'Rack', 'Max', 'David'],
    'Marks':[100,100, 100,100],
    'Subject': ['Math', 'Math', 'Music', 'Physic']
}
 
df = pd.DataFrame(Student_dict)

print('origanl dataframe :\n',df)

#copy dataframe
copieddf = df.copy(deep=False)

print('\n copies dataframe deep copy:\n',copieddf)
 

Output

origanl dataframe :
     Name  Marks Subject
0   Jack    100    Math
1   Rack    100    Math
2    Max    100   Music
3  David    100  Physic

 copies dataframe deep copy:
     Name  Marks Subject
0   Jack    100    Math
1   Rack    100    Math
2    Max    100   Music
3  David    100  Physic

3. Shallow copy change in original Dataframe


To create a shallow copy of the dataframe we can use the dataframe.copy() or dataframe.copy(deep = False).In this example, we are creating a shallow copy of the dataframe,but whenever we made any change in the copy object it also changes the original object data.

Let us understand by an example

Program Example

import pandas as pd
 
Student_dict = {
    'Name': ['Jack', 'Rack', 'Max', 'David'],
    'Marks':[100,100, 100,100],
    'Subject': ['Math', 'Math', 'Music', 'Physic']
}
 
df = pd.DataFrame(Student_dict)

print('\n origanl dataframe :\n',df)

#copy dataframe
copieddf = df.copy(deep=False)

print()

print('\n copies dataframe deep copy:\n',copieddf)


print('\n ----change made in copy also change original dataframe------')
 
#making change in copied dataframe
copieddf.iloc[[0,0,1],:] = 0

print('\n copies dataframe deep copy:\n',copieddf)

print('\n shallow copy chnaged in origanl dataframe :\n',df)

Output

 origanl dataframe :
     Name  Marks Subject
0   Jack    100    Math
1   Rack    100    Math
2    Max    100   Music
3  David    100  Physic


 copies dataframe deep copy:
     Name  Marks Subject
0   Jack    100    Math
1   Rack    100    Math
2    Max    100   Music
3  David    100  Physic

 ----change made in copy also change original dataframe------

 copies dataframe deep copy:
     Name  Marks Subject
0      0      0       0
1      0      0       0
2    Max    100   Music
3  David    100  Physic

 shallow copy chnaged in origanl dataframe :
     Name  Marks Subject
0      0      0       0
1      0      0       0
2    Max    100   Music
3  David    100  Physic

4. Copy columns to new Dataframe


In this example, we have copied specific columns to a new data frame by using the copy() method of dataframe.

Program Example

import pandas as pd
 
Student_dict = {
    'Name': ['Jack', 'Rack', 'Max', 'David'],
    'Marks':[100,100, 100,100],
    'Subject': ['Math', 'Math', 'Music', 'Physic']
}
 
df = pd.DataFrame(Student_dict)

print('\n origanl dataframe :\n',df)

columns_to_copy = df[["Name","Marks"]]

#copy column to dataframe
newdf = columns_to_copy.copy()

print()

print('\n new dataframe after copy columns:\n',newdf)

Output

 origanl dataframe :
     Name  Marks Subject
0   Jack    100    Math
1   Rack    100    Math
2    Max    100   Music
3  David    100  Physic


 new dataframe after copy columns:
     Name  Marks
0   Jack    100
1   Rack    100
2    Max    100
3  David    100

Summary

In this post, we have explored methods to copy Dataframe in Pandas that include a deep and shallow copy.