pandas 为什么这段代码不作为父代码的一部分工作,而是在测试代码中工作?

93ze6v8z  于 2023-04-28  发布在  其他
关注(0)|答案(1)|浏览(97)

我在PyCharm环境下工作。
the parent code here
使用的文件为here
当我在测试环境中运行示例文件时:

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)

# Creating selection functions
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"]):  # Fixed
        return "NA"
    else:
        return "No Match"

predicted['selection'] = predicted.apply(selection, axis=1)
predicted['selection_match'] = predicted.apply(selection_match, axis=1)

它的工作,即列['DNB'][U_4_5]得到更新的测试代码,但他们没有得到更新完全在父代码。即他们的工作,为大部分的列,但其中一些是不受影响时,逻辑,我无法理解为什么。

qoefvg9y

qoefvg9y1#

行正在被更新,但是我可以看到一个条件被应用了一次。你需要一个while loop来循环这些条件。

# Selection Update Loop
while (predicted['selection_match'] == 'no match').any():
    # Modifying selection weights
    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 == 'DNB' and x.result != 'draw' and x.selection_match == 'no match' and x.result_match == 'no match' 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)

    # Checking if the selection matches
    predicted['selection'] = predicted.apply(selection, axis=1)
    predicted['selection_match'] = predicted.apply(selection_match, axis=1)

相关问题