Curious Student
Curious Student

Reputation: 695

Editing row of one dataframe appears to affect another? Pandas Python

Having a bit of a strange issue, where I must be making some sort of mistake.

My script is as follows: Create one dataframe of 4 rows and 3 columns, full of random values. Then create a second dataframe which is equal to the first one, and then in the second df, change the values of the second row.

import pandas as pd
import numpy as np

#Create random df
df1 = pd.DataFrame(np.random.rand(4,3))

#Create a second df which is the same as df1
df2 = df1

#Viewing them both, the look the same
df1
df2

#Now edit row #1 of df2
df2.iloc[1:2] = [[0 , 0, 1]]

#Now viewing them both, both df1 and df2 have row #1 as 0, 0, 1

I thought that df2 = df1 would be a one time assignment and the two dataframes would be entirely separate from then on?

Upvotes: 3

Views: 1238

Answers (1)

jezrael
jezrael

Reputation: 863186

For new mutable object in python (here DataFrame) need copy:

df2 = df1.copy()

Better explanation is here.

Upvotes: 1

Related Questions