python - make a copy of dataframe inside function without changing original -
i'm trying create function can change values of dataframe copy without changing original dataframe. have 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 call function with
home_undervalued(df)
it seems work, realize values df['total_games'] , df['total_wins'] have changed. i'm trying change values local_df, preserve values df. ideas on how fix this?
local_df = df
creates reference df
named local_df
. if want create whole other dataframe (which, way, not recommend) can create new dataframe df.copy(deep=true)
Comments
Post a Comment