我正在尝试创建一个可以在不更改原始数据帧的情况下更改数据框副本值的函数。这就是我到目前为止所做的:
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。关于如何解决这个问题的任何想法?
答案 0 :(得分:5)
local_df = df
只会创建对名为df
的{{1}}的引用。如果你想创建一个完整的其他dataFrame(顺便提一下,我不推荐)你可以创建一个新的dataFrame作为local_df
答案 1 :(得分:3)