💡 Outline
You can copy DataFrame in Pandas usingcopy()method ofDataFrame. By default, it will create deep copy of DataFrame.import pandas as pd df = pd.DataFrame([['Jay','M',21],['Jennifer','F',17], ['Preity','F',19],['Neil','M',17]], By only using the assignment operator=, we are not creating a copy of a DataFrame. It will only create a view of the original and act as another name to access the DataFrame like a pointer. See the following example.import pandas as pd df = pd.DataFrame([['Jay','M',21],['Jennifer','F',17], ['Preity','F',19],['Neil','M',17]], columns = ['Name','Gender','Age']) df1 = df df1.iloc[0,0] = 'Mark' print(df)Output:
Name Gender Age 0 Mark M 21 1 Jennifer F 17 2 Preity F 19 3 Neil M 17As visible in the output, the changes we made to the
df1DataFrame are reflected in the originaldfDataFrame. To create copies of a DataFrame, we can use thecopy()function provided by thepandasmodule.We have two different copies available in Python. A shallow copy and a deep copy.
A
shallow copycreates a reference to the original object. Any changes made to this copy are affected in the original object as well.However, a
deep copyrecursively adds all the data of the original object to a new object. This way any changes made in the deep copy are not reflected in the original copy.## Using copy() method to deep copy
DataFramein Pandas
By default, thecopyfunction creates a deep copy. It has adeepparameter, which is set to True by default.For example,
import pandas as pd df = pd.DataFrame([['Jay','M',21],['Jennifer','F',17], ['Preity','F',19],['Neil','M',17]], columns = ['Name','Gender','Age']) df1 = df.copy() df1.iloc[0,0] = 'Mark' print(df) print(df1)Output:
Name Gender Age 0 Jay M 21 1 Jennifer F 17 2 Preity F 19 3 Neil M 17 Name Gender Age 0 Mark M 21 1 Jennifer F 17 2 Preity F 19 3 Neil M 17In the above example, a deep copy of the original
DataFramewas created. Changes made to this copy are not reflected in the original.## Using copy() method to shallow copy DataFrame in Pandas
If we set thedeepparameter as false, a shallow copy is created.See the following code.
import pandas as pd df = pd.DataFrame([['Jay','M',21],['Jennifer','F',17], ['Preity','F',19],['Neil','M',17]], columns = ['Name','Gender','Age']) df1 = df.copy(deep = False) df1.iloc[0,0] = 'Mark' print(df) print(df1)Output:
Name Gender Age 0 Mark M 21 1 Jennifer F 17 2 Preity F 19 3 Neil M 17 Name Gender Age 0 Mark M 21 1 Jennifer F 17 2 Preity F 19 3 Neil M 17We can also use the earlier discussed assignment operator
=to create shallow copies of DataFrames.That's all about how to copy DataFrame in Pandas.
Copy DataFrame in Pandas