Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

make a copy of dataframe inside function without changing original

Tags:

python

pandas

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?

like image 206
user3294779 Avatar asked Dec 24 '22 23:12

user3294779


1 Answers

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)

like image 142
Eric M. Avatar answered May 21 '23 23:05

Eric M.