python - Pandas Dataframe exists outside of method. Why -
lets presume have simple dataframe df
, simple method dataframe
def alterdf(df): df1['new column'] = df['some column'] + x return df1
in above method modify entire column
x , save new variable name...inside method! however, when inspect original dataframe (i.e. df) see has new column added it...
i aware original dataframe
created exists outside of method. expect alterations occur inside method, should remain there, unless save changes via return
block in method.
however, know wrong...the changes applied within method, occur outside of method. how can be? why so?
probably because have line this
df1 = df # doing copy reference
if want copy dataframe use
df1 = df.copy()
instead
Comments
Post a Comment