search
Search
Login
Math ML Join our weekly DS/ML newsletter
menu
menu search toc more_vert
Robocat
Guest 0reps
Thanks for the thanks!
close
chevron_left Miscellaneous
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
help Ask a question
Share on Twitter
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
A
A
brightness_medium
share
arrow_backShare
Twitter
Facebook
chevron_left Miscellaneous
check_circle
Mark as learned
thumb_up
0
thumb_down
0
chat_bubble_outline
0
auto_stories new
settings

Pandas DataFrame | copy method

Pandas
chevron_right
Documentation
chevron_right
DataFrame
chevron_right
Miscellaneous
schedule Jul 1, 2022
Last updated
local_offer PythonPandas
Tags

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
mail
Join our newsletter for updates on new DS/ML comprehensive guides (spam-free)
robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
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!