Reputation: 695
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