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
home_undervalued(df)
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?
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With