{"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":"!pip install bar_chart_race\nimport bar_chart_race as bcr\nimport pandas as pd\nimport numpy as np\nimport json\nfrom IPython.display import HTML\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-06-26T04:35:15.759435Z","iopub.execute_input":"2021-06-26T04:35:15.759837Z","iopub.status.idle":"2021-06-26T04:35:24.813633Z","shell.execute_reply.started":"2021-06-26T04:35:15.75975Z","shell.execute_reply":"2021-06-26T04:35:24.812491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"players = pd.read_csv('../input/mlb-player-digital-engagement-forecasting/players.csv', usecols=['playerId', 'playerName'])\ntrain = pd.read_csv('../input/mlb-player-digital-engagement-forecasting/train.csv', usecols=['nextDayPlayerEngagement'])\nfor c in ['nextDayPlayerEngagement']:\n    train.loc[pd.notnull(train[c]), c] = train.loc[pd.notnull(train[c]), c].map(lambda x: json.loads(x))\n    \ntarget_df = pd.DataFrame(list(np.concatenate(train['nextDayPlayerEngagement'].values)))\ntarget_df['engagementMetricsDate'] = pd.to_datetime(target_df['engagementMetricsDate'])\ntarget_df = target_df.merge(players, on='playerId', how='left')\ntarget_df","metadata":{"execution":{"iopub.status.busy":"2021-06-26T04:35:24.815812Z","iopub.execute_input":"2021-06-26T04:35:24.816137Z","iopub.status.idle":"2021-06-26T04:36:42.566146Z","shell.execute_reply.started":"2021-06-26T04:35:24.816105Z","shell.execute_reply":"2021-06-26T04:36:42.564882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_bcr_html(df, target_col):\n    df['cummean'] = df.groupby('playerName')[target_col].transform(lambda x: x.cumsum()) / (df.groupby('playerName').cumcount() + 1)\n    df = df[df['engagementMetricsDate']>'2019-01-01']\n    pivot_df = df[['engagementMetricsDate', 'playerName', 'cummean']].pivot_table(\n        index='engagementMetricsDate', \n        columns='playerName',\n        values='cummean',\n    )\n    bcr_html = bcr.bar_chart_race(df=pivot_df, filename=None, n_bars=20, figsize=(6, 6), period_length=100, title=f'{target_col}: Top 20 Engagement Player Bar Chart Race from 2019')\n    return bcr_html","metadata":{"execution":{"iopub.status.busy":"2021-06-26T04:36:42.568718Z","iopub.execute_input":"2021-06-26T04:36:42.569073Z","iopub.status.idle":"2021-06-26T04:36:42.576183Z","shell.execute_reply.started":"2021-06-26T04:36:42.569033Z","shell.execute_reply":"2021-06-26T04:36:42.575257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bcr_html = create_bcr_html(target_df, 'target1')\nHTML(bcr_html.data.replace('<video width=\"1008\" height=\"864\" controls autoplay loop>', '<video width=\"720\" height=\"864\" controls autoplay loop>'))","metadata":{"execution":{"iopub.status.busy":"2021-06-26T04:36:42.577679Z","iopub.execute_input":"2021-06-26T04:36:42.578157Z","iopub.status.idle":"2021-06-26T04:38:50.582352Z","shell.execute_reply.started":"2021-06-26T04:36:42.578109Z","shell.execute_reply":"2021-06-26T04:38:50.581444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bcr_html = create_bcr_html(target_df, 'target2')\nHTML(bcr_html.data.replace('<video width=\"1008\" height=\"864\" controls autoplay loop>', '<video width=\"720\" height=\"864\" controls autoplay loop>'))","metadata":{"execution":{"iopub.status.busy":"2021-06-26T04:38:50.583488Z","iopub.execute_input":"2021-06-26T04:38:50.583787Z","iopub.status.idle":"2021-06-26T04:38:50.588592Z","shell.execute_reply.started":"2021-06-26T04:38:50.583759Z","shell.execute_reply":"2021-06-26T04:38:50.587511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bcr_html = create_bcr_html(target_df, 'target3')\nHTML(bcr_html.data.replace('<video width=\"1008\" height=\"864\" controls autoplay loop>', '<video width=\"720\" height=\"864\" controls autoplay loop>'))","metadata":{"execution":{"iopub.status.busy":"2021-06-26T04:38:50.589913Z","iopub.execute_input":"2021-06-26T04:38:50.590374Z","iopub.status.idle":"2021-06-26T04:38:50.604255Z","shell.execute_reply.started":"2021-06-26T04:38:50.590307Z","shell.execute_reply":"2021-06-26T04:38:50.603395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bcr_html = create_bcr_html(target_df, 'target4')\nHTML(bcr_html.data.replace('<video width=\"1008\" height=\"864\" controls autoplay loop>', '<video width=\"720\" height=\"864\" controls autoplay loop>'))","metadata":{"execution":{"iopub.status.busy":"2021-06-26T04:38:50.605649Z","iopub.execute_input":"2021-06-26T04:38:50.606156Z","iopub.status.idle":"2021-06-26T04:38:50.622589Z","shell.execute_reply.started":"2021-06-26T04:38:50.6061Z","shell.execute_reply":"2021-06-26T04:38:50.621625Z"},"trusted":true},"execution_count":null,"outputs":[]}]}