Created
May 23, 2021 16:39
-
-
Save isaacharrisholt/0ae4bd129c52ef0ce8ba778d868c63ba to your computer and use it in GitHub Desktop.
Code snippet for finding surebets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Formula to find surebets in dataframes | |
def find_surebets(surebet_df, market): | |
# Separate odds into separate columns and clean | |
# x column | |
surebet_df[[f'{market}_x_1', | |
f'{market}_x_2']] = surebet_df[f'{market}_x'].apply(utils.replace_comma).str.split('\n', expand=True) \ | |
.iloc[:, 0:2].apply(pd.Series) | |
surebet_df[f'{market}_x_1'] = surebet_df[f'{market}_x_1'].apply(utils.convert_odds).astype(float) | |
surebet_df[f'{market}_x_2'] = surebet_df[f'{market}_x_2'].apply(utils.convert_odds).astype(float) | |
# y column | |
surebet_df[[f'{market}_y_1', | |
f'{market}_y_2']] = surebet_df[f'{market}_y'].apply(utils.replace_comma).str.split('\n', expand=True) \ | |
.iloc[:, 0:2].apply(pd.Series) | |
surebet_df[f'{market}_y_1'] = surebet_df[f'{market}_y_1'].apply(utils.convert_odds).astype(float) | |
surebet_df[f'{market}_y_2'] = surebet_df[f'{market}_y_2'].apply(utils.convert_odds).astype(float) | |
# Add reciprocals of odds pairs | |
surebet_df[f'{market}_surebets_1'] = (1 / surebet_df[f'{market}_x_1']) + (1 / surebet_df[f'{market}_y_2']) | |
surebet_df[f'{market}_surebets_2'] = (1 / surebet_df[f'{market}_x_2']) + (1 / surebet_df[f'{market}_y_1']) | |
# Clean frame | |
surebet_df = surebet_df[['Competitors_x', f'{market}_x', 'Competitors_y', f'{market}_y', f'{market}_surebets_1', | |
f'{market}_surebets_2']] | |
# Remove non-surebets and reset index | |
surebet_df = surebet_df[(surebet_df[f'{market}_surebets_1'] < 1) | (surebet_df[f'{market}_surebets_2'] < 1)] | |
surebet_df.reset_index(drop=True, inplace=True) | |
return surebet_df |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment