user3294779
user3294779

Reputation: 623

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

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?

Upvotes: 7

Views: 17738

Answers (2)

Eric M.
Eric M.

Reputation: 662

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)

Upvotes: 13

kvorobiev
kvorobiev

Reputation: 5070

Just use copy method of DataFrame

local_df = df.copy(deep=True)

After this line in local_df will be stored copy of df.

Upvotes: 7

Related Questions