{"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":"![NFL](https://static.clubs.nfl.com/image/private/t_editorial_landscape_12_desktop/steelers/xk5boplpxmrobtfa0u0r)","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nfrom datetime import datetime, date\n\nimport plotly.express as px\nfrom plotly.subplots import make_subplots\nimport plotly.figure_factory as ff\nimport plotly.offline as offline\nimport plotly.graph_objs as go\noffline.init_notebook_mode(connected = True)\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-10-03T18:36:50.982038Z","iopub.execute_input":"2021-10-03T18:36:50.982506Z","iopub.status.idle":"2021-10-03T18:36:52.796404Z","shell.execute_reply.started":"2021-10-03T18:36:50.982450Z","shell.execute_reply":"2021-10-03T18:36:52.795782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Dataset Description ","metadata":{}},{"cell_type":"markdown","source":"### Games Data\n> The games file ```games.csv``` includes information about the <b>764 teams</b> playing in each games. \n\n📝 The key variable is gameId.\n\n\n| Name                       | Description                                                                                                                                                                                                                                                                                                                    |\n|----------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| <b>gameId</b>                    | Consits of unique Id (numeric)                                                                                                                                                                                                                                                                                           |\n| <b>gameDate</b>                        | Consits of Game Date                                                                                                                                                                                                                                                                                               |\n| <b>gameTimeEastern</b>               | Starting time of the game                                                                                                                                                                                                                                                                                                  |\n| <b>homeTeamAbbr</b>      | Home Team                                                                                                                                                                                                                                                                                                   |\n| <b>visitorTeamAbbr</b>                  | Visitors Team                                                                                                                                                                                                                                                                                  |\n| <b>week</b> | Week when the games was played |\n| <b>season</b> | Consits the season when the games were played\n| <b>month</b> | Month when the games was played |\n\n                                                                                         \n\n> The idea of the table was taken [from](https://www.kaggle.com/ruchi798/covid-19-impact-on-digital-learning-eda-w-b) \n","metadata":{}},{"cell_type":"code","source":"# game data\ndf_game = pd.read_csv('../input/nfl-big-data-bowl-2022/games.csv')\ndf_game['gameDate'] = pd.to_datetime(df_game['gameDate'])\ndf_game['month'] = df_game['gameDate'].dt.month\ndf_game[:2]","metadata":{"execution":{"iopub.status.busy":"2021-10-03T18:36:52.800628Z","iopub.execute_input":"2021-10-03T18:36:52.800841Z","iopub.status.idle":"2021-10-03T18:36:52.828570Z","shell.execute_reply.started":"2021-10-03T18:36:52.800795Z","shell.execute_reply":"2021-10-03T18:36:52.827844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Players Data\n> The players file ```players.csv``` includes information about the <b>2731</b> players playing in games. \n\n\n| Name                       | Description                                                |\n|----------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| <b>nflld</b>                      | Players identification number; having unique number                                                                                                                                                                                                                                                                                           |\n| <b>height</b>                        | Players height                                                                                                                                                                                                                                                                                               |\n| <b>weight</b>               | Players weight                                                                                                                                                                                                                                                                                                   |\n| <b>birthDate</b>      | Players birthdate                                                                                                                                                                                                                                                                                                   |\n| <b>collegeName</b>                  | Players college Name                                                                                                                                                                                                                                                                                  |\n| <b>Position</b> | Players poisition in the game |\n| <b>displayName</b> | Players name |\n| <b>age</b> | Players age |\n| <b>agegroup</b> | Players agegroup |\n|                            |                                                                                                                                                                                ","metadata":{}},{"cell_type":"code","source":"# player's data\n# adding 2 features age and agegroup\n\n# data\ndf_player = pd.read_csv('../input/nfl-big-data-bowl-2022/players.csv')\ndf_player['birthDate'] = pd.to_datetime(df_player['birthDate'])\n\n# Adding age feature in players data\ntoday = datetime.today()\ndf_player['age'] = df_player['birthDate'].apply(\n               lambda x: today.year - x.year - \n               ((today.month, today.day) < (x.month, x.day)) \n               )\n\n# Adding agegroup feature in players data\nbins= [21.0,26.0,31.0,36.0,41.0,46.0,51.0]\nlabels = ['21-25','26-30','31-35','36-40','41-45','46-50']\ndf_player['agegroup'] = pd.cut(df_player['age'], bins=bins, labels=labels, right=False)\ndf_player[:2]","metadata":{"execution":{"iopub.status.busy":"2021-10-03T18:36:52.829773Z","iopub.execute_input":"2021-10-03T18:36:52.830015Z","iopub.status.idle":"2021-10-03T18:36:52.882177Z","shell.execute_reply.started":"2021-10-03T18:36:52.829989Z","shell.execute_reply":"2021-10-03T18:36:52.881132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plays data\ndf_plays = pd.read_csv('../input/nfl-big-data-bowl-2022/plays.csv')\ndf_plays[:2]","metadata":{"execution":{"iopub.status.busy":"2021-10-03T18:36:52.884287Z","iopub.execute_input":"2021-10-03T18:36:52.884532Z","iopub.status.idle":"2021-10-03T18:36:53.000591Z","shell.execute_reply.started":"2021-10-03T18:36:52.884503Z","shell.execute_reply":"2021-10-03T18:36:52.999750Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# tracking data - 2018\ndf_trac2018 = pd.read_csv('../input/nfl-big-data-bowl-2022/tracking2018.csv')\ndf_trac2018[:2]","metadata":{"execution":{"iopub.status.busy":"2021-10-03T18:36:53.001857Z","iopub.execute_input":"2021-10-03T18:36:53.002110Z","iopub.status.idle":"2021-10-03T18:37:35.433751Z","shell.execute_reply.started":"2021-10-03T18:36:53.002058Z","shell.execute_reply":"2021-10-03T18:37:35.432748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Visualization","metadata":{}},{"cell_type":"code","source":"# data\ndf_sea = pd.DataFrame(df_game['season'].value_counts()).reset_index().rename(columns={'index':'season','season':'count'})\n\ndf_gd2018 = pd.DataFrame(df_game[df_game['season']==2018]['month'].value_counts()).reset_index().rename(columns={'index':'month','month':'count'}).sort_values(by='month')\ndf_gd2019 = pd.DataFrame(df_game[df_game['season']==2019]['month'].value_counts()).reset_index().rename(columns={'index':'month','month':'count'}).sort_values(by='month')\ndf_gd2020 = pd.DataFrame(df_game[df_game['season']==2020]['month'].value_counts()).reset_index().rename(columns={'index':'month','month':'count'}).sort_values(by='month')\ndf_gd2018['month'] =['9M','10M','11M','12M']\ndf_gd2019['month'] =['9M','10M','11M','12M']\ndf_gd2020['month'] =['1M','9M','10M','11M','12M']\n\n# chart color\ndf_gd2018['color'] = '#c6ccd8'\ndf_gd2018['color'][3:] = '#496595'\ndf_gd2019['color'] = '#a8b1c3'\ndf_gd2019['color'][3:] = '#496595'\ndf_gd2020['color'] = '#99a3b9'\ndf_gd2020['color'][4:] = '#496595'\n\n# chart\nfig = make_subplots(rows=1, cols=4, column_widths=[0.26,0.26,0.26,0.2],\n                    specs=[[{\"type\": \"bar\"},{\"type\": \"bar\"},{\"type\": \"bar\"},{\"type\": \"pie\"}]],\n                    subplot_titles=(\"2018 Season\", \"2019 Season\", \"2020 Season\"))\nfig.add_trace(go.Bar(x=df_gd2018['count'], y=df_gd2018['month'], name='2018', \n                     marker=dict(color= df_gd2018['color']), orientation='h'),\n                     row=1, col=1)\nfig.add_trace(go.Bar(x=df_gd2019['count'], y=df_gd2019['month'], name='2019',\n                     marker=dict(color= df_gd2019['color']), orientation='h'),\n              row=1, col=2)\nfig.add_trace(go.Bar(x=df_gd2020['count'], y=df_gd2020['month'], name='2020', \n                     marker=dict(color= df_gd2020['color']), orientation='h'),\n              row=1, col=3)\nfig.add_trace(go.Pie(values=df_sea['count'], labels=df_sea['season'], name='Season',\n                    marker=dict(colors=['#99a3b9','#a8b1c3','#c6ccd8']), hole=0.7,\n                    rotation=90, hoverinfo='label+percent', textinfo='label'),\n              row=1, col=4)\nfig.update_yaxes(showgrid=False, ticksuffix=' ')\nfig.update_xaxes(visible=False)\nfig.update_layout(height=550, bargap=0.2,\n                  margin=dict(b=0,r=0),\n                  hovermode=\"y unified\",\n                  title_text=\"Most played Games: Season wiseMonth\",\n                  plot_bgcolor='#fff', paper_bgcolor='#fff',\n                  title_font=dict(size=25, color='#8a8d93', family=\"Lato, sans-serif\"),\n                  font=dict(color='#8a8d93'),\n                  hoverlabel=dict(bgcolor=\"#f2f2f2\", font_size=13, font_family=\"Lato, sans-serif\"),\n                  showlegend=False\n                 )\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2021-10-03T18:37:35.435840Z","iopub.execute_input":"2021-10-03T18:37:35.436079Z","iopub.status.idle":"2021-10-03T18:37:35.621177Z","shell.execute_reply.started":"2021-10-03T18:37:35.436052Z","shell.execute_reply":"2021-10-03T18:37:35.620535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = px.histogram(df_game, x='week', color_discrete_sequence=['#85a1c1'],\n                   title='Distribution of Week', height=350, \n                   template='plotly_white'\n                  )\nfig.update_yaxes(visible=False)\nfig.update_traces(hovertemplate=None, marker=dict(line=dict(width=0)))\nfig.update_layout(margin=dict(b=0, l=20, r=30),\n                  hovermode=\"x unified\", bargap=0.2, xaxis_title=\" \",\n                  title_font=dict(size=25, color='#8a8d93', family=\"Lato, sans-serif\"),\n                  font=dict(color='#8a8d93'),\n                  hoverlabel=dict(bgcolor=\"#f2f2f2\", font_size=13, font_family=\"Lato, sans-serif\"),\n                  xaxis = dict(tickmode = 'array',\n                               tickvals = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17],\n                               ticktext = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17])\n                 )\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2021-10-03T18:37:35.622385Z","iopub.execute_input":"2021-10-03T18:37:35.622874Z","iopub.status.idle":"2021-10-03T18:37:35.884240Z","shell.execute_reply.started":"2021-10-03T18:37:35.622837Z","shell.execute_reply":"2021-10-03T18:37:35.883326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Interpret\n___\nMost of the games were played in 2020 in November month.<br>\nGenerally games are played in the month September to January\n___","metadata":{}},{"cell_type":"code","source":"df_s_vta = pd.crosstab(df_game['season'], df_game['visitorTeamAbbr']).T.reset_index()\ndf_s_hta = pd.crosstab(df_game['season'], df_game['homeTeamAbbr']).T.reset_index()\ndf_s_vta.columns=['visitorTeamAbbr','2018V','2019V','2020V']\ndf_s_hta.columns=['homeTeamAbbr','2018H','2019H','2020H']\n\n\nfig = make_subplots(rows=1, cols=3, shared_yaxes = True, horizontal_spacing = 0,\n                    subplot_titles=(\"2018 Season\", \"2019 Season\", \"2020 Season\"))\nfig.add_trace(go.Bar(y=df_s_hta['homeTeamAbbr'], x=df_s_hta['2018H'], marker=dict(color='#c6ccd8'),\n                     name='Home', orientation='h'),\n              row=1, col=1)\nfig.add_trace(go.Bar(y=df_s_vta['visitorTeamAbbr'], x=df_s_vta['2018V'], marker=dict(color='#496595'),\n                     name='Visitor', orientation='h'),\n              row=1, col=1)\nfig.add_trace(go.Bar(y=df_s_hta['homeTeamAbbr'], x=df_s_hta['2019H'], marker=dict(color='#c6ccd8'),\n                     name='Home', orientation='h'),\n              row=1, col=2)\nfig.add_trace(go.Bar(y=df_s_vta['visitorTeamAbbr'], x=df_s_vta['2019V'], marker=dict(color='#496595'),\n                     name='Visitor', orientation='h'),\n              row=1, col=2)\nfig.add_trace(go.Bar(y=df_s_hta['homeTeamAbbr'], x=df_s_hta['2020H'], marker=dict(color='#c6ccd8'),\n                     name='Home', orientation='h'),\n              row=1, col=3)\nfig.add_trace(go.Bar(y=df_s_vta['visitorTeamAbbr'], x=df_s_vta['2020V'], marker=dict(color='#496595'),\n                     name='Visitor', orientation='h'),\n              row=1, col=3)\n\n\nfig.update_xaxes(visible=False)\nfig.update_yaxes(ticksuffix=' ')\ntitle = \"No of Games for every Season: <span style='color:#c6ccd8'>Home <span style='color:#8a8d93'>& <span style='color:#496595'>Visitors\"\nfig.update_layout(title=title,\n                  height=800, bargap=0.3, \n                  hovermode=\"y unified\",\n                  plot_bgcolor='#fff', paper_bgcolor='#fff',\n                  title_font=dict(size=25, color='#8a8d93', family=\"Lato, sans-serif\"),\n                  font=dict(color='#8a8d93'),\n                  hoverlabel=dict(bgcolor=\"#f2f2f2\", font_size=13, font_family=\"Lato, sans-serif\"),\n                  margin=dict(t=100, b=0, l=80, r=40),\n                  showlegend=False\n                 )\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2021-10-03T18:37:35.885608Z","iopub.execute_input":"2021-10-03T18:37:35.885933Z","iopub.status.idle":"2021-10-03T18:37:36.005902Z","shell.execute_reply.started":"2021-10-03T18:37:35.885892Z","shell.execute_reply":"2021-10-03T18:37:36.005290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_gt = df_game.sort_values(by='gameTimeEastern')\ndf_gt['hour'] = df_gt['gameTimeEastern'].apply(lambda x: int(x.split(':')[0]))\nfig = px.histogram(df_gt, x='hour', height = 350, color_discrete_sequence=['#85a1c1'],\n                   title='Distribution of Game Time(Hour)',\n                   template='plotly_white'\n                  )\nfig.update_yaxes(visible=False)\nfig.update_traces(hovertemplate=None, marker=dict(line=dict(width=0)))\nfig.update_layout(hovermode=\"x unified\", xaxis_title=\" \", bargap=0.2,\n                  title_font=dict(size=25, color='#8a8d93', family=\"Lato, sans-serif\"),\n                  font=dict(color='#8a8d93'),\n                  hoverlabel=dict(bgcolor=\"#f2f2f2\", font_size=13, font_family=\"Lato, sans-serif\"),\n                  margin=dict(b=0),\n                  xaxis = dict(tickmode = 'array',\n                               tickvals = [9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22],\n                               ticktext = [9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22])\n                 )\nfig.add_vrect(x0=9.7, x1=11.4,\n              xref = \"paper\",\n              yref = \"y2\",\n              fillcolor= \"#e9cf87\",\n              opacity=0.25, \n              line_width=0\n)\nfig.add_annotation(x=10.4, y=100,\n            text=\"<b>0</b> Hour\",\n            showarrow=False)\nfig.add_vrect(x0=13.6, x1=14.4,\n              xref = \"paper\",\n              yref = \"y2\",\n              fillcolor= \"#e9cf87\",\n              opacity=0.25, \n              line_width=0\n)\nfig.add_annotation(x=14, y=100,\n            text=\"<b>0</b><br>Hour\",\n            showarrow=False)\nfig.add_vrect(x0=17.6, x1=18.4,\n              xref = \"paper\",\n              yref = \"y2\",\n              fillcolor= \"#e9cf87\",\n              opacity=0.25, \n              line_width=0\n)\nfig.add_annotation(x=18, y=100,\n            text=\"<b>0</b><br>Hour\",\n            showarrow=False)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2021-10-03T18:37:36.007087Z","iopub.execute_input":"2021-10-03T18:37:36.007876Z","iopub.status.idle":"2021-10-03T18:37:36.120043Z","shell.execute_reply.started":"2021-10-03T18:37:36.007833Z","shell.execute_reply":"2021-10-03T18:37:36.119171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = px.histogram(df_player, y='Position', height=600, template='plotly_white',\n                   color_discrete_sequence=['#85a1c1'], title='Players count by position')\nfig.update_xaxes(visible=False)\nfig.update_yaxes(showgrid=False, categoryorder='total ascending', ticksuffix=' ', showline=False)\nfig.update_traces(hovertemplate=None, marker=dict(line=dict(width=0)))\nfig.update_layout(hovermode=\"y unified\", yaxis_title=\" \",\n                  title_font=dict(size=25, color='#8a8d93', family=\"Lato, sans-serif\"),\n                  font=dict(color='#8a8d93'),\n                  hoverlabel=dict(bgcolor=\"#f2f2f2\", font_size=13, font_family=\"Lato, sans-serif\"),\n                  showlegend=False,\n                  margin=dict(b=10)\n                 )\nfig.add_hrect(y0=19.6, y1=25.4,\n              xref = \"paper\",\n              yref = \"y2\",\n              fillcolor= \"#e9cf87\",\n              opacity=0.25, \n              line_width=0\n)\nfig.add_annotation(x=300, y=21.5,\n            text=\"<b>50%</b> of Players<br>lies here\",\n            showarrow=False)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2021-10-03T18:37:36.121572Z","iopub.execute_input":"2021-10-03T18:37:36.121802Z","iopub.status.idle":"2021-10-03T18:37:36.236122Z","shell.execute_reply.started":"2021-10-03T18:37:36.121774Z","shell.execute_reply":"2021-10-03T18:37:36.235282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_cn = pd.DataFrame(df_player['collegeName'].value_counts()[:20]).reset_index().rename(columns={'index':'collegeName','collegeName':'count'})\nfig = px.histogram(df_cn, x='count', y='collegeName', color_discrete_sequence=['#85a1c1'],\n                   title='Top 10 Colleges having highest players')\nfig.update_xaxes(visible=False)\nfig.update_yaxes(showgrid=False, categoryorder='total ascending', ticksuffix=' ', showline=False)\nfig.update_traces(hovertemplate=None, marker=dict(line=dict(width=0)))\nfig.update_layout(hovermode=\"y unified\", yaxis_title=\" \",\n                  plot_bgcolor='#fff', paper_bgcolor='#fff',\n                  title_font=dict(size=25, color='#8a8d93', family=\"Lato, sans-serif\"),\n                  font=dict(color='#8a8d93'),\n                  hoverlabel=dict(bgcolor=\"#f2f2f2\", font_size=13, font_family=\"Lato, sans-serif\"),\n                  showlegend=False,\n                  margin=dict(b=10)\n                 )\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2021-10-03T18:37:36.237395Z","iopub.execute_input":"2021-10-03T18:37:36.237632Z","iopub.status.idle":"2021-10-03T18:37:36.327899Z","shell.execute_reply.started":"2021-10-03T18:37:36.237604Z","shell.execute_reply":"2021-10-03T18:37:36.327061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_pa = df_player[['Position','agegroup']].dropna()\nfig = px.histogram(df_pa, y='Position', color='agegroup', height=550,\n                   color_discrete_sequence=['#c6ccd8', '#a8b1c3', '#99a3b9', '#85a1c1', '#222'],\n                   title='Distribution of Players Age vs Position')\nfig.update_xaxes(visible=False)\nfig.update_yaxes(showgrid=False, categoryorder='total ascending', ticksuffix=' ', showline=False)\nfig.update_traces(hovertemplate=None, marker=dict(line=dict(width=0)))\nfig.update_layout(hovermode=\"y unified\", yaxis_title=\" \",\n                  plot_bgcolor='#fff', paper_bgcolor='#fff',\n                  title_font=dict(size=25, color='#8a8d93', family=\"Lato, sans-serif\"),\n                  font=dict(color='#8a8d93'), bargap=0.1,\n                  hoverlabel=dict(bgcolor=\"#f2f2f2\", font_size=13, font_family=\"Lato, sans-serif\"),\n                #  showlegend=False,\n                  margin=dict(b=0)\n                 )\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2021-10-03T18:37:36.329239Z","iopub.execute_input":"2021-10-03T18:37:36.329502Z","iopub.status.idle":"2021-10-03T18:37:36.474663Z","shell.execute_reply.started":"2021-10-03T18:37:36.329469Z","shell.execute_reply":"2021-10-03T18:37:36.473803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## If you like the plotly charts do upvote\nI have lot of things to analyze I will be keep updating. ","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}