Home > database >  Python: Why is this if-else code incorrect?
Python: Why is this if-else code incorrect?

Time:12-11

My df:

 match_datetime                  country                          league             home_team_x                away_team_x  home_odds_x  draw_odds_x  away_odds_x  predicted_home_score  predicted_away_score  predicted_btts  predicted_class_btts  predicted_bttso2.5  predicted_class_bttso2.5  predicted_bttsu2.5  predicted_class_bttsu2.5  predicted_o2.5  predicted_class_o2.5  predicted_u2.5  predicted_class_u2.5  predicted_o1.5  predicted_class_o1.5  predicted_u1.5  predicted_class_u1.5                    run_time  predicted_o4  predicted_class_o4  predicted_u4  predicted_class_u4  predicted_draw  predicted_class_draw  predicted_total_score  predicted_score_difference    Win    DNB  O 1.5  U 4.5  Unnamed: 0  home_odds_y  draw_odds_y  away_odds_y             datetime             home_team_y                away_team_y  home_score  away_score  selection predicted_result result  total_score  score_difference result_match selection_match
index                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
0      2021-08-06 06:00:00                   Russia                 FNL 2 - Group 2               Yenisey 2          Lokomotiv-Kazanka         2.61         3.51         2.32                  1.45                  1.73            0.66                     1                0.56                         0                0.11                         0            0.62                     1            0.33                     0            0.76                     1            0.24                     0  2021-08-07 00:04:36.326391           NaN                 NaN           NaN                 NaN             NaN                   NaN                   3.18                        0.28  1.040  0.650  3.100  2.400   1036746.0         2.50         3.37         2.49  2021-08-06 06:00:00               Yenisey 2          Lokomotiv-Kazanka         2.0         1.0      O 1.5             Away   Home          3.0               1.0     No Match           Match
1      2021-08-06 07:00:00                   Russia                    Youth League                Ural U19  Krylya Sovetov Samara U19         2.10         3.68         2.98                  1.25                  1.33            0.44                     0                0.40                         0                0.04                         0            0.56                     1            0.47                     0            0.60                     1            0.35                     0  2021-08-07 00:04:36.326391           NaN                 NaN           NaN                 NaN             NaN                   NaN                   2.58                        0.08  1.040  0.650  3.100  2.400   1067198.0         2.20         3.70         2.81  2021-08-06 07:00:00                Ural U19  Krylya Sovetov Samara U19         1.0         1.0          N             Away   Draw          2.0               0.0     No Match         No Sel.
2      2021-08-06 08:00:00                    World                   Club Friendly                  Alaves                    Al Nasr         1.94         3.63         3.42                  1.84                  1.39            0.68                     1                0.42                         1                0.19                         0            0.64                     1            0.42                     0            0.66                     1            0.34                     0  2021-08-07 00:04:36.326391           NaN                 NaN           NaN                 NaN             NaN                   NaN                   3.23                        0.45  1.040  0.650  3.100  2.400         NaN          NaN          NaN          NaN                  NaN                     NaN                        NaN         NaN         NaN      O 1.5             Home   None          NaN               NaN     No Match        No Match
3      2021-08-06 08:30:00                    China                      Jia League       Chengdu Rongcheng          Shenyang Urban FC         1.16         6.87        15.29                  3.07                  0.52            0.50                     0                0.37                         0                0.04                         0            0.81                     1            0.26                     0            0.84                     1            0.10                     0  2021-08-07 00:04:36.326391           NaN                 NaN           NaN                 NaN             NaN                   NaN                   3.59                        2.55  2.420  1.500  4.800  2.200   1020431.0         1.16         7.09        14.93  2021-08-06 08:30:00       Chengdu Rongcheng          Shenyang Urban FC         2.0         0.0          W             Home   Home          2.0               2.0        Match           Match
4      2021-08-06 08:30:00                    China                    Super League                Wuhan FC       Tianjin Jinmen Tiger         1.56         3.69         6.71                  1.90                  0.68            0.50                     0                0.40                         0                0.21                         0            0.51                     0            0.58                     1            0.63                     1            0.37                     0  2021-08-07 00:04:36.326391           NaN                 NaN           NaN                 NaN             NaN                   NaN                   2.58                        1.22  1.040  0.660  3.100  2.400   1074682.0         1.68         3.48         5.60  2021-08-06 08:30:00                Wuhan FC       Tianjin Jinmen Tiger         1.0         1.0          W             Home   Draw          2.0               0.0     No Match        No Match

i am trying this code for the column selection_match as:

def selection_match(row):
    if row["selection"] == "N":
        return "No Sel."
    if (row["home_score"]   row["away_score"]) < 5 and row["selection"] == "U 4.5":
        return "Match"
    if row["result"] == row["predicted_result"] and row["selection"] == "W":
        return "Match"
    if row["result"] == row["predicted_result"] and row["total_score"] > 1 and row["selection"] == "W & O 1.5":
        return "Match"
    if row["total_score"] > 1 and row["selection"] == "O 1.5":
        return "Match"
    if (row["result"] == row["predicted_result"] or row["result"] == 'Draw') and row["selection"] == "DNB":
        return "Match"
    if row["home_score"] == "NaN": # I would like a way to check if NA and then return value but I am unable to get this right
        return "NA"
    else:
        return "No Match"

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

I am getting incorrect responses

                       country                          league               home_team                  away_team  predicted_home_score  predicted_away_score  predicted_total_score  predicted_score_difference    Win    DNB  home_score  away_score  selection result  total_score  score_difference result_match
index                                                                                                                                                                                                                                                                                                               
0                       Russia                 FNL 2 - Group 2               Yenisey 2          Lokomotiv-Kazanka                  1.45                  1.73                   3.18                        0.28  1.040  0.650         2.0         1.0      O 1.5   Home          3.0               1.0     No Match
1                       Russia                    Youth League                Ural U19  Krylya Sovetov Samara U19                  1.25                  1.33                   2.58                        0.08  1.040  0.650         1.0         1.0          N   Draw          2.0               0.0     No Match
2                        World                   Club Friendly                  Alaves                    Al Nasr                  1.84                  1.39                   3.23                        0.45  1.040  0.650         NaN         NaN      O 1.5   None          NaN               NaN     No Match
3                        China                      Jia League       Chengdu Rongcheng          Shenyang Urban FC                  3.07                  0.52                   3.59                        2.55  2.420  1.500         2.0         0.0          W   Home          2.0               2.0        Match
4                        China                    Super League                Wuhan FC       Tianjin Jinmen Tiger                  1.90                  0.68                   2.58                        1.22  1.040  0.660         1.0         1.0          W   Draw          2.0               0.0     No Match

How can I write the correct if-else code for this instance?

CodePudding user response:

You should structure your conditional statements as if..elif...elif.. else. Also to check for NaN you can use the isna() function.

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"

df_tableau['selection_match'] = df_tableau.apply(selection_match, axis=1)
  • Related