user3294779 user3294779 - 1 year ago 155
Python Question

make a copy of dataframe inside function without changing original

I'm trying to create a function that can change the values of a dataframe copy without changing the original dataframe. This is what I have so far:

def home_undervalued(df):
local_df = df
local_df['total_games'] = 0
local_df['total_wins'] = 0
cond_1 = local_df['predicted_spread'] > local_df['vegas_spread']
cond_2 = local_df['actual_spread'] > local_df['vegas_spread']
cond_3 = local_df['predicted_spread'] - local_df['vegas_spread'] >= 3

local_df.loc[cond_1 & cond_3 , 'total_games'] = 1
local_df.loc[cond_1 & cond_2 & cond_3 , 'total_wins'] = 1

total_games = sum(local_df.total_games)
total_wins = sum(local_df.total_wins)

return float(total_wins) / float(total_games)

I then call the function with


It seems to work, but then I realize the values for df['total_games'] and df['total_wins'] have changed. I'm trying to change the values for local_df, but preserve the values df. Any ideas on how to fix this?

Answer Source

local_df = df just creates a reference to df named local_df. If you want to create a whole other dataFrame (which, by the way, I do not recommend) you can just create a new dataFrame as df.copy(deep=True)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download