python – 在函数内部复制dataframe而不更改原始数据

我正在尝试创建一个可以在不更改原始数据帧的情况下更改数据帧副本值的函数.这是我到目前为止:

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)

然后我调用函数

home_undervalued(df)

它似乎工作,但后来我意识到df [‘total_games’]和df [‘total_wins’]的值已经改变.我正在尝试更改local_df的值,但保留值df.有想法该怎么解决这个吗?

最佳答案 local_df = df只是创建一个名为local_df的df的引用.如果你想创建一个完整的其他dataFrame(顺便说一句,我不推荐)你可以创建一个新的dataFrame作为df.copy(deep = True)

点赞