Pandas .copy() method is used to create a copy of a Pandas object. Variables are also used to generate copy of an object but variables are just pointer to an object and any change in new data will also change the previous data.
The following examples will show the difference between copying through variables and Pandas.copy() method.
Example #1: Copying through variables
In this example, a sample Pandas series is made and copied in a new variable. After that, some changes are made in the new data and compared with the old data.
As shown in the output image, the changes made in new data are also reflected in the old data since the new variable was just a pointer to old one.
Example #2: Using Pandas.copy() method
In this example, pandas.copy() method is used to copy a data and some changes are made in the new data. The changes are then compared to old data.
As shown in the output image, the changes in new data are independent and didn’t change anything in old one.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Python | Cloning or Copying a list
- Array Copying in Python
- MoviePy – Shallow copying Video File Clip
- Python - Pearson Correlation Test Between Two Variables
- Visualizing Relationship between variables with scatter plots in Seaborn
- Global and Local Variables in Python
- Python | Set 2 (Variables, Expressions, Conditions and Functions)
- How to assign values to variables in Python and other languages
- Python | Intersect two dictionaries through keys
- Python | Iterate through value lists dictionary
- Python - Iterate through list without using the increment variable
- Visualising ML DataSet Through Seaborn Plots and Matplotlib
- Class or Static Variables in Python
- Python | Extract key-value of dictionary in variables
- Private Variables in Python
- Python | Assign multiple variables with list values
- Python program to find number of local variables in a function
- Inserting variables to database table using Python
- How are variables stored in Python - Stack or Heap?
- Python | Unpack whole list into variables
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.