{"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":"markdown","source":"# Big Data Derby 2022: Merge and Convert Data to Parquet File\n\n\nThis notebook does the following:\n\n1. combine the 3 tables into 1 big table\n1. create `race_id` for 'race_date', 'race_number' as the unique identifier of each race\n1. create `race_horse_id` for 'race_date', 'race_number', 'program_number' as the unique identifier for each race + horse ","metadata":{}},{"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\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-09-25T06:05:38.467796Z","iopub.execute_input":"2022-09-25T06:05:38.468176Z","iopub.status.idle":"2022-09-25T06:05:38.476839Z","shell.execute_reply.started":"2022-09-25T06:05:38.468146Z","shell.execute_reply":"2022-09-25T06:05:38.475419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pyarrow.parquet as pq\nimport pyarrow as pa","metadata":{"execution":{"iopub.status.busy":"2022-09-25T06:05:39.571573Z","iopub.execute_input":"2022-09-25T06:05:39.571998Z","iopub.status.idle":"2022-09-25T06:05:39.577748Z","shell.execute_reply.started":"2022-09-25T06:05:39.571959Z","shell.execute_reply":"2022-09-25T06:05:39.576604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"race_table = pd.read_csv('/kaggle/input/big-data-derby-2022/nyra_race_table.csv')\nstart_table = pd.read_csv('/kaggle/input/big-data-derby-2022/nyra_start_table.csv', header=None)\ntracking_table = pd.read_csv('/kaggle/input/big-data-derby-2022/nyra_tracking_table.csv')\n\nstart_table.columns=['track_id', 'race_date', 'race_number', 'program_number', 'weight_carried', 'jockey', 'odds', 'position_at_finish']\n","metadata":{"execution":{"iopub.status.busy":"2022-09-25T06:05:40.889092Z","iopub.execute_input":"2022-09-25T06:05:40.890237Z","iopub.status.idle":"2022-09-25T06:05:49.567511Z","shell.execute_reply.started":"2022-09-25T06:05:40.890196Z","shell.execute_reply":"2022-09-25T06:05:49.566315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"race_table.shape, start_table.shape, tracking_table.shape","metadata":{"execution":{"iopub.status.busy":"2022-09-25T06:08:20.974031Z","iopub.execute_input":"2022-09-25T06:08:20.974482Z","iopub.status.idle":"2022-09-25T06:08:20.982669Z","shell.execute_reply.started":"2022-09-25T06:08:20.974451Z","shell.execute_reply":"2022-09-25T06:08:20.981721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"race_table.sort_values(by=['race_date', 'race_number'], ascending=[True]*2, inplace=True)\nrace_table['race_id']=range(1, race_table.shape[0]+1)","metadata":{"execution":{"iopub.status.busy":"2022-09-25T06:10:57.188593Z","iopub.execute_input":"2022-09-25T06:10:57.189028Z","iopub.status.idle":"2022-09-25T06:10:57.205546Z","shell.execute_reply.started":"2022-09-25T06:10:57.188992Z","shell.execute_reply":"2022-09-25T06:10:57.204343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"start_table.sort_values(by=['race_date', 'race_number', 'program_number'], ascending=[True]*3, inplace=True)\nstart_table['race_horse_id']=range(1, start_table.shape[0]+1)","metadata":{"execution":{"iopub.status.busy":"2022-09-25T06:11:36.269460Z","iopub.execute_input":"2022-09-25T06:11:36.271380Z","iopub.status.idle":"2022-09-25T06:11:36.288958Z","shell.execute_reply.started":"2022-09-25T06:11:36.271323Z","shell.execute_reply":"2022-09-25T06:11:36.287770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = race_table.merge(start_table, on=['track_id', 'race_date', 'race_number'], how='inner')\nprint(df.shape)\ndf = df.merge(tracking_table, on=['track_id', 'race_date', 'race_number', 'program_number'], how='inner')\nprint(df.shape)","metadata":{"execution":{"iopub.status.busy":"2022-09-25T06:11:45.754692Z","iopub.execute_input":"2022-09-25T06:11:45.755088Z","iopub.status.idle":"2022-09-25T06:11:48.878460Z","shell.execute_reply.started":"2022-09-25T06:11:45.755058Z","shell.execute_reply":"2022-09-25T06:11:48.877212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head(2)","metadata":{"execution":{"iopub.status.busy":"2022-09-25T06:11:52.216399Z","iopub.execute_input":"2022-09-25T06:11:52.217137Z","iopub.status.idle":"2022-09-25T06:11:52.237546Z","shell.execute_reply.started":"2022-09-25T06:11:52.217100Z","shell.execute_reply":"2022-09-25T06:11:52.236391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pq.write_table(pa.Table.from_pandas(df), 'nyra_2019_complete.parquet', compression = 'GZIP')\n    \n","metadata":{"execution":{"iopub.status.busy":"2022-09-25T06:11:52.630154Z","iopub.execute_input":"2022-09-25T06:11:52.630620Z","iopub.status.idle":"2022-09-25T06:12:12.437001Z","shell.execute_reply.started":"2022-09-25T06:11:52.630586Z","shell.execute_reply":"2022-09-25T06:12:12.435580Z"},"trusted":true},"execution_count":null,"outputs":[]}]}