J.A.Cado J.A.Cado - 10 months ago 37
Python Question

Pandas Dataframe exists outside of method. Why

Lets presume a I have a simple dataframe


And a simple method that does something to the

def alterDF(df):
df1['new column'] = df['some column'] + x
return df1

In the above method I modify an entire
with x and save it to a new variable name...inside the method!
However, when I inspect my original dataframe (i.e. df) I see that it also has the new column added to it...

I am aware that the original
I created exists outside of the method. But I would expect that any alterations that occur inside the method, should remain there, unless I save the changes via the
block in my method.

However, I know that I am wrong...the changes also applied within my method, also occur outside of my method. How can this be? Why is this so?


Probably because you have a line like this

df1 = df # by doing this you copy the reference also

if you want to copy a dataframe use

df1 = df.copy()