Copy DataFrame in Pandas

Copy DataFrame in Python

💡 Outline
You can copy DataFrame in Pandas using copy() method of DataFrame. By default, it will create deep copy of DataFrame.

import pandas as pd
df = pd.DataFrame([['Jay','M',21],['Jennifer','F',17],
                   ['Preity','F',19],['Neil','M',17]], 

By only using the assignment operator =, we are not creating a copy of a DataFrame. It will only create a view of the original and act as another name to access the DataFrame like a pointer.

See the following example.

import pandas as pd
df = pd.DataFrame([['Jay','M',21],['Jennifer','F',17],
                   ['Preity','F',19],['Neil','M',17]], 
                  columns = ['Name','Gender','Age'])
df1 = df
df1.iloc[0,0] = 'Mark'
print(df)    

Output:

       Name Gender  Age
0      Mark      M   21
1  Jennifer      F   17
2    Preity      F   19
3      Neil      M   17

As visible in the output, the changes we made to the df1 DataFrame are reflected in the original df DataFrame. To create copies of a DataFrame, we can use the copy() function provided by the pandas module.

We have two different copies available in Python. A shallow copy and a deep copy.

A shallow copy creates a reference to the original object. Any changes made to this copy are affected in the original object as well.

However, a deep copy recursively adds all the data of the original object to a new object. This way any changes made in the deep copy are not reflected in the original copy.

## Using copy() method to deep copy DataFrame in Pandas
By default, the copy function creates a deep copy. It has a deep parameter, which is set to True by default.

For example,

import pandas as pd
df = pd.DataFrame([['Jay','M',21],['Jennifer','F',17],
                   ['Preity','F',19],['Neil','M',17]], 
                  columns = ['Name','Gender','Age'])
df1 = df.copy()
df1.iloc[0,0] = 'Mark'
print(df)
print(df1)    

Output:

       Name Gender  Age
0       Jay      M   21
1  Jennifer      F   17
2    Preity      F   19
3      Neil      M   17
       Name Gender  Age
0      Mark      M   21
1  Jennifer      F   17
2    Preity      F   19
3      Neil      M   17    

In the above example, a deep copy of the original DataFrame was created. Changes made to this copy are not reflected in the original.

## Using copy() method to shallow copy DataFrame in Pandas
If we set the deep parameter as false, a shallow copy is created.

See the following code.

import pandas as pd
df = pd.DataFrame([['Jay','M',21],['Jennifer','F',17],
                   ['Preity','F',19],['Neil','M',17]], 
                  columns = ['Name','Gender','Age'])
df1 = df.copy(deep = False)
df1.iloc[0,0] = 'Mark'
print(df)
print(df1)    

Output:

       Name Gender  Age
0      Mark      M   21
1  Jennifer      F   17
2    Preity      F   19
3      Neil      M   17
       Name Gender  Age
0      Mark      M   21
1  Jennifer      F   17
2    Preity      F   19
3      Neil      M   17    

We can also use the earlier discussed assignment operator = to create shallow copies of DataFrames.

That's all about how to copy DataFrame in Pandas.

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.