为何某段Pandas代码在测试环境正常,嵌入主代码后更新不完全?
问题描述
我在PyCharm环境中工作,测试环境运行以下示例代码时,DNB和U_4_5列能正常更新;但将这段代码嵌入主代码后,这两列仅部分行得到更新,其余行未受影响,逻辑一致却无法定位原因。
测试代码
import pandas as pd df = pd.read_csv("predicted_no_sel.csv") df = df.loc[:, ['total_score', 'away_score', 'home_score', 'Win', 'DNB', 'O_1_5', 'U_4_5', 'predicted_score_difference', 'predicted_total_score', 'result', 'predicted_result', 'result_match', 'selection', 'selection_match']] predicted = df predicted['Win'] = predicted.apply(lambda x: x.predicted_score_difference + 0.02 if x.selection_match == "No Match" and x.selection in ["W", "W & O 1.5"] and x.result != x.predicted_result and x.result != 'Draw' else(x.predicted_score_difference + 0.02 if x.selection_match == "No Match" and x.selection == "W & O 1.5" and x.result == 'Draw' else(x.predicted_score_difference + 0.02 if x.selection_match == "No Match" and x.selection == "W" and x.result != 'Draw' else(x.predicted_score_difference + 0.02 if x.selection_match == "No Match" and x.selection == "W" and x.result == 'Draw' else x.Win))), axis=1) predicted['DNB'] = predicted.apply(lambda x: x.predicted_score_difference + 0.02 if x.selection_match == "No Match" and x.selection == "DNB"else (x.predicted_score_difference + 0.02 if x.selection_match == "No Match" and x.selection == "W & O 1.5" and x.result != x.predicted_result and x.result != 'Draw'else (x.predicted_score_difference + 0.02 if x.selection_match == "No Match" and x.selection == "W" and x.result != x.predicted_result and x.result != 'Draw'else (x.predicted_score_difference + 0.02 if x.selection_match == "No Match" and x.selection == "DNB" and x.result != x.predicted_result and x.result != 'Draw' else x.DNB))), axis=1) predicted['O_1_5'] = predicted.apply(lambda x: x.predicted_total_score + 0.02 if x.selection_match == "No Match" and (x.selection == "O 1.5" or x.selection == "W & O 1.5") and x.total_score < 2 else x['O_1_5'], axis=1) predicted['U_4_5'] = predicted.apply(lambda x: x.predicted_total_score - 0.02 if (x.total_score > 4) and (x.selection == "U 4.5") else x['U_4_5'], axis=1) # 创建选择函数 def selection(row): if row["predicted_score_difference"] > row["Win"] and row["predicted_total_score"] > row["O_1_5"]: return "W & O 1.5" if row["predicted_score_difference"] > row["Win"]: return "W" if row["predicted_total_score"] > row["O_1_5"]: return "O 1.5" if row["predicted_score_difference"] > row["DNB"] and row["predicted_score_difference"] < row["Win"] and row[ "predicted_total_score"] > row["O_1_5"]: return "O 1.5 or DNB" if row["predicted_score_difference"] > row["DNB"] and row["predicted_score_difference"] < row["Win"]: return "DNB" if row["predicted_score_difference"] > row["Win"] and row["predicted_total_score"] < row["U_4_5"]: return "W & U 4.5" if row["predicted_total_score"] < row["U_4_5"]: return "U 4.5" if row["predicted_score_difference"] < row["DNB"]: return "N" def selection_match(row): if row["selection"] == "N": return "No Sel." elif (row["home_score"] + row["away_score"]) < 5 and row["selection"] == "U 4.5": return "Match" elif row["result"] == row["predicted_result"] and row["selection"] == "W": return "Match" elif row["result"] == row["predicted_result"] and row["total_score"] > 1 and row["selection"] == "W & O 1.5": return "Match" elif row["total_score"] > 1 and row["selection"] == "O 1.5": return "Match" elif (row["result"] == row["predicted_result"] or row["result"] == 'Draw') and row["selection"] == "DNB": return "Match" elif pd.isna(row["home_score"]): # 已修复 return "NA" else: return "No Match" predicted['selection'] = predicted.apply(selection, axis=1) predicted['selection_match'] = predicted.apply(selection_match, axis=1)
内容的提问来源于stack exchange,提问作者PyNoob
相关产品推荐
相关产品推荐

