{"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\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":"2021-07-28T04:36:41.626918Z","iopub.execute_input":"2021-07-28T04:36:41.627777Z","iopub.status.idle":"2021-07-28T04:36:41.650889Z","shell.execute_reply.started":"2021-07-28T04:36:41.627675Z","shell.execute_reply":"2021-07-28T04:36:41.649494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Other important imports\n\nimport json\nfrom pandas.io.json import json_normalize\n","metadata":{"execution":{"iopub.status.busy":"2021-07-28T06:27:08.704722Z","iopub.execute_input":"2021-07-28T06:27:08.705435Z","iopub.status.idle":"2021-07-28T06:27:08.710939Z","shell.execute_reply.started":"2021-07-28T06:27:08.705374Z","shell.execute_reply":"2021-07-28T06:27:08.70967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Load the data:","metadata":{}},{"cell_type":"code","source":"train_data = pd.read_csv(\"/kaggle/input/mlb-player-digital-engagement-forecasting/train.csv\")","metadata":{"execution":{"iopub.status.busy":"2021-07-28T04:57:39.186031Z","iopub.execute_input":"2021-07-28T04:57:39.186534Z","iopub.status.idle":"2021-07-28T04:59:04.842499Z","shell.execute_reply.started":"2021-07-28T04:57:39.186498Z","shell.execute_reply":"2021-07-28T04:59:04.84124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"player_data = pd.read_csv(\"/kaggle/input/mlb-player-digital-engagement-forecasting/players.csv\")\nteam_data = pd.read_csv(\"/kaggle/input/mlb-player-digital-engagement-forecasting/teams.csv\")\nseason_data = pd.read_csv(\"/kaggle/input/mlb-player-digital-engagement-forecasting/seasons.csv\")\naward_data = pd.read_csv(\"/kaggle/input/mlb-player-digital-engagement-forecasting/awards.csv\")","metadata":{"execution":{"iopub.status.busy":"2021-07-28T05:01:41.278538Z","iopub.execute_input":"2021-07-28T05:01:41.278955Z","iopub.status.idle":"2021-07-28T05:01:41.32955Z","shell.execute_reply.started":"2021-07-28T05:01:41.278919Z","shell.execute_reply":"2021-07-28T05:01:41.328108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_train_data = train_data.iloc[[100]]\ntrain_columns = sample_train_data.columns\n#print(sample_train_data['playerBoxScores'][100])\nx = json.loads(sample_train_data['playerBoxScores'][100])\nprint(json_normalize(x))\nprint(train_columns)\nprint(type(train_columns))\n\nnested_train_columns = train_columns.drop('date')","metadata":{"execution":{"iopub.status.busy":"2021-07-28T06:36:07.349558Z","iopub.execute_input":"2021-07-28T06:36:07.350011Z","iopub.status.idle":"2021-07-28T06:36:07.525834Z","shell.execute_reply.started":"2021-07-28T06:36:07.349974Z","shell.execute_reply":"2021-07-28T06:36:07.524804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"netsed_train_columns = ['nextDayPlayerEngagement', 'games', 'rosters', 'playerBoxScores',\n       'teamBoxScores', 'transactions', 'standings', 'awards', 'events',\n       'playerTwitterFollowers', 'teamTwitterFollowers']","metadata":{"execution":{"iopub.status.busy":"2021-07-28T06:28:03.791635Z","iopub.execute_input":"2021-07-28T06:28:03.792106Z","iopub.status.idle":"2021-07-28T06:28:03.798328Z","shell.execute_reply.started":"2021-07-28T06:28:03.792044Z","shell.execute_reply":"2021-07-28T06:28:03.796923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create a function to output nested JSON data given a column(s) from the training set as  dataframe \n# This function only considers one date at a time\n\ndef jsonToDf(column):\n    assert(column in netsed_train_columns)\n    ","metadata":{"execution":{"iopub.status.busy":"2021-07-28T06:28:49.853285Z","iopub.execute_input":"2021-07-28T06:28:49.853958Z","iopub.status.idle":"2021-07-28T06:28:49.858692Z","shell.execute_reply.started":"2021-07-28T06:28:49.853897Z","shell.execute_reply":"2021-07-28T06:28:49.857592Z"},"trusted":true},"execution_count":null,"outputs":[]}]}