Advertisement
PyNoob1

Step-4-Normalizing 888Sport Odds

Jun 11th, 2021
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.05 KB | None | 0 0
  1. import pandas as pd
  2. from ast import literal_eval
  3. from datetime import timedelta
  4. pd.set_option('display.max_rows', 5000)
  5. pd.set_option('display.max_columns', 500)
  6. pd.set_option('display.width', 1000)
  7. from tabulate import tabulate
  8.  
  9. print("Reading raw file")
  10. df = pd.read_csv(r"C:\Users\harsh\Google Drive\sportsintel.shop\Files\Output\odds_raw.csv")
  11.  
  12. print("Rearranging columns")
  13. df = df[['time', 'League', 'home_team', 'away_team', 'full_time_result', 'both_teams_to_score', 'double_chance']]
  14. df['time'] = df['time'].apply(lambda a: pd.to_datetime(a).date())
  15. df.iloc[:, 4:] = df.iloc[:, 4:].applymap(literal_eval)
  16.  
  17. df_list = list()
  18.  
  19. for col in df.columns[4:]:
  20.     v = pd.json_normalize(df[col])
  21.     v.columns = [f'{col}_{c}' for c in v.columns]
  22.     df_list.append(v)
  23.  
  24. df_normalized = pd.concat([df.iloc[:, :4]] + df_list, axis=1)
  25. df_normalized.time = pd.to_datetime(df_normalized.time)
  26. print("Converting time to UK time")
  27. df_normalized['time'] = pd.to_datetime(df_normalized['time']) - timedelta(hours=1)
  28. df_normalized = df_normalized.dropna()
  29.  
  30. integers = {
  31.     'full_time_result_1': int,
  32.     'full_time_result_X': int,
  33.     'full_time_result_2': int,
  34.     'both_teams_to_score_yes': int,
  35.     'both_teams_to_score_no': int,
  36.     'double_chance_1X': int,
  37.     'double_chance_12': int,
  38.     'double_chance_2X': int
  39. }
  40. df_normalized = df_normalized.astype(integers)
  41.  
  42. df_normalized = df_normalized.loc[:, df_normalized.columns.intersection(
  43.     ['time', 'League', 'home_team', 'away_team', 'full_time_result_1', 'full_time_result_X', 'full_time_result_2'])]
  44.  
  45. df_normalized = df_normalized.set_axis(['DateTime', 'League', 'HomeTeam', 'AwayTeam', 'B365H', 'B365D', 'B365A'],
  46.                                        axis=1, inplace=False)
  47.  
  48. df_normalized['B365H'] = df_normalized['B365H'].div(1000)
  49. df_normalized['B365D'] = df_normalized['B365D'].div(1000)
  50. df_normalized['B365A'] = df_normalized['B365A'].div(1000)
  51. print("Writing to excel")
  52.  
  53.  
  54. def write_excel(filename, sheetname, dataframe):
  55.     with pd.ExcelWriter(filename, engine='openpyxl', mode='a') as writer:
  56.         workBook = writer.book
  57.         try:
  58.             workBook.remove(workBook[sheetname])
  59.         except:
  60.             print("Worksheet does not exist")
  61.         finally:
  62.             dataframe.to_excel(writer, sheet_name=sheetname, index=False)
  63.             writer.save()
  64.  
  65.  
  66. write_excel(r"C:\Users\harsh\Google Drive\sportsintel.shop\Files\Output\Backtest.xlsx", '888', df_normalized)
  67. print(df_normalized)
  68.  
  69. print("Reconciling with team dictionary names")
  70.  
  71. df_odds = df_normalized
  72.  
  73. df_dict = pd.read_excel(r"C:\Users\harsh\Google Drive\sportsintel.shop\Files\Output\Backtest.xlsx",
  74.                         sheet_name='Team Dict_888', engine='openpyxl')
  75.  
  76. df_dict = df_dict.dropna()
  77.  
  78. s = df_dict.set_index('888_Names')['Team (Dataset)']
  79. df_odds['HomeTeam'] = df_odds['HomeTeam'].map(s)
  80. df_odds['AwayTeam'] = df_odds['AwayTeam'].map(s)
  81.  
  82. print(tabulate(df_odds, headers='keys'))
  83.  
  84. write_excel(r"C:\Users\harsh\Google Drive\sportsintel.shop\Files\Output\Backtest.xlsx", 'Testing', df_normalized)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement