Signup/Sign In
PUBLISHED ON: MAY 3, 2021

Python pandas Series.copy() Method

In this tutorial, we will learn the Python pandas Series.copy() method. This method can be used to make a copy of the Series object’s indices and its data before doing any manipulation or changes to the Series. It returns the Series object which is the copy of the caller Series.

The below shows the syntax of the Series.copy() method.

Syntax

Series.copy(deep=True)

Parameters

deep: It represents the bool(True or False), and the default is True. It makes a deep copy of the Series, including a copy of the data and the indices. When deep=False neither the indices nor the data are copied.

Example: Copy the Series using the Series.copy() Method

Here, in this example, we defined Series with different values, and using the Series.copy() method we copied the Series with its data and printed the output. See the below example.

#importing pandas as pd
import pandas as pd
#importing numpy as np
import numpy as np
series_1 = pd.Series([1,2,np.nan,4])
print("-------The series is------")
print(series_1)
print("--------copy of the series is-------")
series_2=series_1.copy()
print(series_2)


-------The series is------
0 1.0
1 2.0
2 NaN
3 4.0
dtype: float64
--------copy of the series is-------
0 1.0
1 2.0
2 NaN
3 4.0
dtype: float64

Example: Copy the Series using the Series.copy() Method

This example is similar to the previous one. Here, we make a copy of the Series and doing the manipulation to the new copied Series object. See the below example.

After copying the Series, we added a value 3 at the index 2 and printed the output. The changes we made in the new Series object do not affect the original Series.

#importing pandas as pd
import pandas as pd
#importing numpy as np
import numpy as np
series_1 = pd.Series([1,2,np.nan,4])
print("-------The series is------")
print(series_1)
print("--------copy of the series is-------")
series_2=series_1.copy()
series_2[2]=3
print(series_2)


-------The series is------
0 1.0
1 2.0
2 NaN
3 4.0
dtype: float64
--------copy of the series is-------
0 1.0
1 2.0
2 3.0
3 4.0
dtype: float64

Example: Set deep=False in the Series.copy() Method

By default the parameter deep in the Series.copy() Method set to True. Here, in this example, we set the parameter deep=True in the Series.copy() Method. Now, this method copies the Series object with indices and data, but if we make any changes to the Series, it will reflect the original Series also. We added the value 3 at the index 2 for the Series_2 only, but manipulation is done in Series_1 also. See the differences in the below example.

#importing pandas as pd
import pandas as pd
#importing numpy as np
import numpy as np
series_1 = pd.Series([1,2,np.nan,4])
print("-------The series is------")
print(series_1)
series_2=series_1.copy(deep=False)
series_2[2]=3
print("-------The series is------")
print(series_1)


-------The series is------
0 1.0
1 2.0
2 NaN
3 4.0
dtype: float64
-------The series is------
0 1.0
1 2.0
2 3.0
3 4.0
dtype: float64

Conclusion

In this tutorial, we learned Python pandas Series.copy() method. We learned the parameter of the Series.copy() method solved examples, and how this method copies the DataFrame with different parameters and differences of these parameters.



About the author:
I like writing about Python, and frameworks like Pandas, Numpy, Scikit, etc. I am still learning Python. I like sharing what I learn with others through my content.