Shallow copy vs Deep copy in Pandas Series - GeeksforGeeks

21 Sep, 2021
The pandas library has mainly two data structures DataFrames and Series. These data structures are internally represented with index arrays, which label the data, and data arrays, which contain the actual data. Now, when we try to copy these data structures (DataFrames and Series) we essentially copy the object’s indices and data and there are two ways to do so, namely Shallow Copy and Deep Copy.
These operations are done with the help of the library functions pandas.Series.copy(deep=False) for shallow copy and pandas.Series.copy(deep=True) for deep copy in Series.
Now, let’s understand what shallow copying is.
When a shallow copy of a Series or Series object is created, it doesn’t copy the indices and the data of the original object but it simply copies the references to its indices and data. As a result of which, a change made to one is reflected in the other one.
It refers to constructing a new collection object and then populating it with references to the child objects found in the original. The copying process does not recurse and therefore won’t create copies of the child objects themselves. 
Example:
Python3
# program to depict shallow copy# in pandas series # import moduleimport pandas as pd # assign seriesser = pd.Series(['Mandy', 'Ron', 'Jacob', 'Bayek'])  # shallow copycopyser = ser.copy(deep=False)  # comparing shallow copied series# and original seriesprint('\nBefore Operation:\n', copyser == ser) # assignment operationcopyser[2] = 'Geeks'  # comparing shallow copied series# and original seriesprint('\nAfter Operation:\n', copyser == ser) print('\nOriginal Dataframe after operation:\n', ser)
Output:
As we can see from the output of the above program, the changes applied to the shallow copied data frame gets automatically applied to the original series.
A deep copy of a Series or a Series object has its own copy of index and data. It is a process in which the copying process occurs recursively. It means first constructing a new collection object and then recursively populating it with copies of the child objects found in the original. In the case of deep copy, a copy of an object is copied into another object. It means that any changes made to a copy of the object do not reflect in the original object. 
Example:
Python3
# program to depict deep copy# in pandas series # import moduleimport pandas as pd # assign seriesser = pd.Series(['Mandy', 'Ron', 'Jacob', 'Bayek'])  # shallow copycopyser = ser.copy(deep=True)  # comparing deep copied series# and original seriesprint('\nBefore Operation:\n', copyser == ser) # assignment operationcopyser[2] = 'Geeks'  # comparing deep copied series# and original seriesprint('\nAfter Operation:\n', copyser == ser) print('\nOriginal Dataframe after operation:\n', ser)
Output:
Here, the data inside the original objects are not recursively copied. That is, the data inside the data of the original objects still point to the same memory unit. For example, if the data in Series object contains any mutable data then it will be shared between it and its deep copy and any modification to one will be reflected in the other one. 
anikaseth98
gabaa406
manikarora059
Python pandas-series
Python-pandas
Technical Scripter 2020
Python
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Python Dictionary
Enumerate() in Python
Iterate over a list in Python
Read a file line by line in Python
Python OOPs Concepts
Different ways to create Pandas Dataframe
sum() function in Python
How to Install PIP on Windows ?
Stack in Python
Bar Plot in Matplotlib