{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\nimport seaborn as sns\nimport matplotlib.pyplot as pyplot\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-11-05T15:23:42.337990Z","iopub.execute_input":"2022-11-05T15:23:42.338494Z","iopub.status.idle":"2022-11-05T15:23:42.356937Z","shell.execute_reply.started":"2022-11-05T15:23:42.338458Z","shell.execute_reply":"2022-11-05T15:23:42.355658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# INTRODUCTION\nThis notebook will explore the winningest jockeys per track condition. ","metadata":{}},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/big-data-derby-2022/nyra_2019_complete.csv', header=None, \n                 names=['track_id','race_date','race_number','program_number','trakus_index','latitude','longitude','distance_id','course_type','track_condition','run_up_distance','race_type','purse', 'post_time','weight_carried','jockey','odds','position_at_finish'], \n                 dtype={'track_id':str,'race_date':str,'race_number':str,'program_number':str,'trakus_index':str,'latitude':str,'longitude':str,'distance_id':int,'course_type':str,'track_condition':str,'run_up_distance':str,'race_type':str,'purse':float,'post_time':str,'weight_carried':int,'jockey':str,'odds':str,'position_at_finish':int},\n                 parse_dates=['race_date'])","metadata":{"execution":{"iopub.status.busy":"2022-11-05T15:23:42.358796Z","iopub.execute_input":"2022-11-05T15:23:42.359387Z","iopub.status.idle":"2022-11-05T15:24:03.849596Z","shell.execute_reply.started":"2022-11-05T15:23:42.359350Z","shell.execute_reply":"2022-11-05T15:24:03.848374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()","metadata":{"execution":{"iopub.status.busy":"2022-11-05T15:24:03.851323Z","iopub.execute_input":"2022-11-05T15:24:03.852723Z","iopub.status.idle":"2022-11-05T15:24:03.867948Z","shell.execute_reply.started":"2022-11-05T15:24:03.852644Z","shell.execute_reply":"2022-11-05T15:24:03.866363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-05T15:24:03.871925Z","iopub.execute_input":"2022-11-05T15:24:03.872604Z","iopub.status.idle":"2022-11-05T15:24:03.897339Z","shell.execute_reply.started":"2022-11-05T15:24:03.872555Z","shell.execute_reply":"2022-11-05T15:24:03.896064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Drop duplicates","metadata":{}},{"cell_type":"code","source":"df.drop(columns = ['trakus_index','latitude','longitude'], inplace=True)\ndf.drop_duplicates(inplace=True)\ndf.drop(columns = ['run_up_distance','post_time','purse','program_number','odds','weight_carried'], inplace=True)\n","metadata":{"execution":{"iopub.status.busy":"2022-11-05T15:24:03.898898Z","iopub.execute_input":"2022-11-05T15:24:03.899768Z","iopub.status.idle":"2022-11-05T15:24:09.958013Z","shell.execute_reply.started":"2022-11-05T15:24:03.899689Z","shell.execute_reply":"2022-11-05T15:24:09.956955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-05T15:24:09.959674Z","iopub.execute_input":"2022-11-05T15:24:09.960370Z","iopub.status.idle":"2022-11-05T15:24:09.976276Z","shell.execute_reply.started":"2022-11-05T15:24:09.960331Z","shell.execute_reply":"2022-11-05T15:24:09.974643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Count only the jockey who finished 1st and 2nd place ","metadata":{}},{"cell_type":"code","source":"df['top_finisher'] = 0\nfilter = df['position_at_finish'] <= 2\ndf.loc[filter,'top_finisher'] = 1","metadata":{"execution":{"iopub.status.busy":"2022-11-05T15:24:09.977873Z","iopub.execute_input":"2022-11-05T15:24:09.979032Z","iopub.status.idle":"2022-11-05T15:24:09.991962Z","shell.execute_reply.started":"2022-11-05T15:24:09.978994Z","shell.execute_reply":"2022-11-05T15:24:09.990965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['race_date_yyyymm'] = df['race_date'].dt.to_period('M')","metadata":{"execution":{"iopub.status.busy":"2022-11-05T15:24:09.995805Z","iopub.execute_input":"2022-11-05T15:24:09.996268Z","iopub.status.idle":"2022-11-05T15:24:10.015645Z","shell.execute_reply.started":"2022-11-05T15:24:09.996227Z","shell.execute_reply":"2022-11-05T15:24:10.014307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()","metadata":{"execution":{"iopub.status.busy":"2022-11-05T15:24:10.018941Z","iopub.execute_input":"2022-11-05T15:24:10.019620Z","iopub.status.idle":"2022-11-05T15:24:10.046914Z","shell.execute_reply.started":"2022-11-05T15:24:10.019577Z","shell.execute_reply":"2022-11-05T15:24:10.045634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head(20)","metadata":{"execution":{"iopub.status.busy":"2022-11-05T15:24:10.048975Z","iopub.execute_input":"2022-11-05T15:24:10.050067Z","iopub.status.idle":"2022-11-05T15:24:10.074292Z","shell.execute_reply.started":"2022-11-05T15:24:10.050026Z","shell.execute_reply":"2022-11-05T15:24:10.073321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"top_groupby_df = df.loc[:, ['track_condition','jockey','top_finisher']].groupby(['track_condition','jockey']).agg({'top_finisher':sum}).reset_index()\ntop_groupby_df.head(20)","metadata":{"execution":{"iopub.status.busy":"2022-11-05T15:24:10.075692Z","iopub.execute_input":"2022-11-05T15:24:10.076755Z","iopub.status.idle":"2022-11-05T15:24:10.110755Z","shell.execute_reply.started":"2022-11-05T15:24:10.076685Z","shell.execute_reply":"2022-11-05T15:24:10.109475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tracks = top_groupby_df['track_condition'].unique()\n\nfor t in tracks:\n    filter = top_groupby_df['track_condition'] == t\n    jockeys = top_groupby_df.loc[filter].nlargest(15, 'top_finisher')['jockey'].to_list()\n    \n    report_filter = (df['track_condition'] == t) & (df['jockey'].isin(jockeys))\n    report_df = df.loc[report_filter, ['race_date_yyyymm','track_condition','jockey','top_finisher']].groupby(['race_date_yyyymm','track_condition','jockey']).agg({'top_finisher':sum}).reset_index()\n    \n    #convert the race_date_yyyymm to str. there is an error when using the Period(M) in sns\n    report_df['race_date_yyyymm'] = report_df['race_date_yyyymm'].astype('str')\n    fig, ax1 = pyplot.subplots(figsize=(20, 10))\n    g = sns.lineplot(x='race_date_yyyymm', y='top_finisher', hue='jockey', data=report_df, ax=ax1).set(title='Track Condition = ' + t)\n    sns.despine(fig)","metadata":{"execution":{"iopub.status.busy":"2022-11-05T15:24:10.112884Z","iopub.execute_input":"2022-11-05T15:24:10.113321Z","iopub.status.idle":"2022-11-05T15:24:13.628681Z","shell.execute_reply.started":"2022-11-05T15:24:10.113285Z","shell.execute_reply":"2022-11-05T15:24:13.627403Z"},"trusted":true},"execution_count":null,"outputs":[]}]}