search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to

Pandas DataFrame | copy method

schedule Aug 12, 2023
Last updated
local_offer
PythonPandas
Tags
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
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 = pd.DataFrame({"A":[1,2], "B":[3,4]})
df
   A  B
0  1  3
1  2  4

We perform deep copying to create df_other, and change its first value to 5:

df_other = df.copy()   # deep=True
df_other.iloc[0,0] = 5
df_other
   A  B
0  5  3
1  2  4

We then check the state of the original DataFrame df:

df
   A  B
0  1  3
1  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 = pd.DataFrame({"A":[[1,2],3], "B":[4,5]})
df
A B
0 [1,2] 4
1 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_other.iloc[0,0][0] = 9      # Modify the first value in the Python list
df
A B
0 [9,2] 4
1 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 = pd.DataFrame({"A":[1,2], "B":[3,4]})
df
   A  B
0  1  3
1  2  4

We perform shallow copying to create df_other, and change its first value to 5:

df_other = df.copy(deep=False)
df_other.iloc[0,0] = 5
df_other
   A  B
0  5  3
1  2  4

We then check the state of the original DataFrame df:

df
   A  B
0  5  3
1  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.iloc[0,0] = 6
df_other
   A  B
0  6  3
1  2  4
robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!