I have a huge If-Else code that I write in Tableau The compiler simply takes a lot of time to execute this code so I want to move it onto Python.
My df:
JavaScript
x
8
1
match_datetime country league home_team away_team predicted_home_score predicted_away_score predicted_total_score predicted_score_difference
2
38342 2021-09-15 09:30:00 Australia FFA Cup Edge Hill Gold Coast Knights 1.007927 1.920937 2.928864 0.913010
3
43807 2021-09-21 09:30:00 Australia FFA Cup Queensland Lions Casuarina 3.333684 0.761920 4.095605 2.571764
4
49031 2021-09-26 05:00:00 Australia FFA Cup Floreat Athena Adelaide United 0.688574 2.832026 3.520600 2.143452
5
53094 2021-09-29 10:00:00 Australia FFA Cup ECU Joondalup Adelaide Olympic 2.042965 1.688064 3.731028 0.354901
6
54080 2021-09-29 10:00:00 Australia FFA Cup ECU Joondalup Adelaide Olympic 1.803334 1.554651 3.357985 0.248683
7
8
I have a VLOOKUP table that Interprets these values to provide an output
JavaScript
1
11
11
1
df_list:
2
Country League Win DNB O 1.5 U 4.5
3
84 Australia A-League 1.45 1.45 3.60 2.2
4
85 Australia A-League Women 1.04 0.65 3.15 2.4
5
86 Australia Brisbane Premier League 1.04 0.65 3.10 2.4
6
87 Australia Capital Territory 1.04 0.65 3.10 2.4
7
88 Australia FFA Cup 1.49 1.49 3.58 2.4
8
9
10
11
My If-Else code simply put is:
JavaScript
1
17
17
1
IF df["country"] = df_list["Country"] AND df["league"] = df_list["League"] AND df["predicted_score_difference"] > df_list["Win"] AND df["predicted_total_score"] > df_list["O 1.5"]
2
THEN "W & O 1.5"
3
ELSEIF df["country"] = df_list["Country"] AND df["league"] = df_list["League"] AND df["predicted_score_difference"] > df_list["Win"]
4
THEN "W"
5
ELSEIF df["country"] = df_list["Country"] AND df["league"] = df_list["League"] AND df["predicted_total_score"] > df_list["O 1.5"]
6
THEN "O 1.5"
7
ELSEIF df["country"] = df_list["Country"] AND df["league"] = df_list["League"] AND df["predicted_score_difference"] > df_list["DNB"] AND df["predicted_score_difference"] < df_list["Win"] AND df["predicted_total_score"] > df_list["O 1.5"]
8
THEN "O 1.5 or DNB"
9
ELSEIF df["country"] = df_list["Country"] AND df["league"] = df_list["League"] AND df["predicted_score_difference"] > df_list["DNB"] AND df["predicted_score_difference"] < df_list["Win"]
10
THEN "DNB"
11
ELSEIF df["country"] = df_list["Country"] AND df["league"] = df_list["League"] AND df["predicted_score_difference"] > df_list["Win"] AND df["predicted_total_score"] < df_list["U 4.5"]
12
THEN "W & U 4.5"
13
ELSEIF df["country"] = df_list["Country"] AND df["league"] = df_list["League"] AND df["predicted_total_score"] < df_list["U 4.5"]
14
THEN "U 4.5"
15
ELSEIF df["country"] = df_list["Country"] AND df["league"] = df_list["League"] AND df["predicted_score_difference"] < df_list["DNB"]
16
THEN "N"
17
Where df_output is the resulting dataframe
e.g. for
JavaScript
1
3
1
match_datetime country league home_team away_team predicted_home_score predicted_away_score predicted_total_score predicted_score_difference
2
38342 2021-09-15 09:30:00 Australia FFA Cup Edge Hill Gold Coast Knights 1.007927 1.920937 2.928864 0.913010
3
The df_output[“result”] would be “DNB”
How can I write the same code in Python to save time?
Advertisement
Answer
Like this?
JavaScript
1
22
22
1
def func(row):
2
if row["predicted_score_difference"] > row["Win"] and row["predicted_total_score"] > row["O 1.5"]:
3
return "W & O 1.5"
4
if row["predicted_score_difference"] > row["Win"]:
5
return "W"
6
if row["predicted_total_score"] > row["O 1.5"]:
7
return "O 1.5"
8
if row["predicted_score_difference"] > row["DNB"] and row["predicted_score_difference"] < row["Win"] and row["predicted_total_score"] > row["O 1.5"]:
9
return "O 1.5 or DNB"
10
if row["predicted_score_difference"] > row["DNB"] and row["predicted_score_difference"] < row["Win"]:
11
return "DNB"
12
if row["predicted_score_difference"] > row["Win"] and row["predicted_total_score"] < row["U 4.5"]:
13
return "W & U 4.5"
14
if row["predicted_total_score"] < row["U 4.5"]:
15
return "U 4.5"
16
if row["predicted_score_difference"] < row["DNB"]:
17
return "N"
18
19
df = df.reset_index().merge(df_list, how="left", left_on=["country", "league"],right_on=["Country", "League"]).set_index('index')
20
df['result'] = df.apply(func,axis=1)
21
print(df)
22
output:
JavaScript
1
8
1
match_datetime country league home_team away_team predicted_home_score predicted_away_score Country League Win DNB O 1.5 U 4.5 result
2
index
3
38342 2021-09-15 09:30:00 Australia FFA Cup Edge Hill Gold Coast Knights 1.007927e+06 1.920937e+06 Australia FFA Cup 1.49 1.49 3.58 2.4 O 1.5
4
43807 2021-09-21 09:30:00 Australia FFA Cup Queensland Lions Casuarina 3.333684e+06 7.619200e-01 Australia FFA Cup 1.49 1.49 3.58 2.4 W & O 1.5
5
49031 2021-09-26 05:00:00 Australia FFA Cup Floreat Athena Adelaide United 6.885740e-01 2.832026e+06 Australia FFA Cup 1.49 1.49 3.58 2.4 W & O 1.5
6
53094 2021-09-29 10:00:00 Australia FFA Cup ECU Joondalup Adelaide Olympic 2.042965e+06 1.688064e+06 Australia FFA Cup 1.49 1.49 3.58 2.4 O 1.5
7
54080 2021-09-29 10:00:00 Australia FFA Cup ECU Joondalup Adelaide Olympic 1.803334e+06 1.554651e+06 Australia FFA Cup 1.49 1.49 3.58 2.4 O 1.5
8