Search code examples
pandasdataframeapply

Why does this code not work as a part of the parent code but works in test code?


I am working in the PyCharm environment.

This is the parent code here:

The files used are here

When I run in the test environment with the sample file:

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)

It works i.e. the columns ['DNB'] and [U_4_5] get updated in the test code but they dont get updated completely in the parent code. i.e. they work for most of the columns but some of them are unaffected when the logic and I am unable to understand why.


Solution

  • The rows are being updated however I can see that one condition is being applied once. You would need a while loop to loop through the conditions.

    # 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)