Pandas DataFrame | copy method
Start your free 7-days trial now!
Pandas DataFrame.copy(~)
method makes a copy of a DataFrame. You can choose whether you want a deep copy or a shallow copy.
A deep copy is an entirely new copy - modifying the deep copy of the DataFrame will not mutate the original DataFrame, and vice versa. The exception is when the values are Python objects; their properties are not recursively copied and hence modifying Python objects in the deep copy would mutate the original, and vice versa.
A shallow copy shares the same memory block as the original DataFrame for its data. This means that if you modify the shallow copy, then the original DataFrame will get mutated and vice versa.
Parameters
1. deep
| boolean
| optional
If
True
, then deep copying is performed.If
False
, then shallow copying is performed.
By default, deep=True
.
Return Value
A DataFrame
that is either a shallow or deep copy of the source DataFrame.
Examples
Deep copying
Consider the following DataFrame:
df
A B0 1 31 2 4
We perform deep copying to create df_other
, and change its first value to 5
:
A B0 5 31 2 4
We then check the state of the original DataFrame df
:
df
A B0 1 31 2 4
We see that the original DataFrame is left intact. This is because we performed deep copying, as opposed to shallow copying.
Exception
Be cautious when the values of the DataFrame are Python objects - their properties are not recursively copied and so modifying them in the deep copy would still mutate the objects in the original DataFrame.
Consider the following DataFrame:
df
A B0 [1,2] 41 3 5
We make a new deep copy of df
, and modify the first value of the list:
df_other = df.copy(deep=True)df
A B0 [9,2] 41 3 5
We can see that the changes in df_other
are reflected in the original df
. The reverse would also be true, that is, modifying the Python list in df
would mutate that in df_other
.
Shallow copying
Consider the following DataFrame:
df
A B0 1 31 2 4
We perform shallow copying to create df_other
, and change its first value to 5:
A B0 5 31 2 4
We then check the state of the original DataFrame df
:
df
A B0 5 31 2 4
We see that the original DataFrame has also been modified, despite the fact that we never directly modified it. This is because we performed shallow copying, as opposed to deep copying.
Moreover, changing df
will also modify df_other
:
df_other
A B0 6 31 2 4