我試圖創建一個函數,可以在不更改原始數據框的情況下更改數據框副本的值。這是我到目前爲止有:在不改變原始數據框的情況下創建數據框副本
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。有想法該怎麼解決這個嗎?
你會推薦什麼,而不是製作新的副本?添加更多列? – user3294779
我會研究爲什麼你認爲你需要一個副本,儘管你可能需要一個副本,但可能會有更高性能的解決方案。添加更多列或替換不需要的現有列是一個好主意。 –
這很有道理。謝謝。 – user3294779