{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"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 in \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 \"../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# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"","_uuid":"","trusted":true},"cell_type":"code","source":"import pandas as pd\nInjuryRecord = pd.read_csv(\"../input/nfl-playing-surface-analytics/InjuryRecord.csv\")\nPlayList = pd.read_csv(\"../input/nfl-playing-surface-analytics/PlayList.csv\")\nPlayerTrackData = pd.read_csv(\"../input/nfl-playing-surface-analytics/PlayerTrackData.csv\")","execution_count":0,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import pandas as pd\nfrom matplotlib import pyplot as plt\nimport seaborn as sns\n\ninjury = pd.read_csv(\"../input/nfl-playing-surface-analytics/InjuryRecord.csv\")\nplaylist = pd.read_csv(\"../input/nfl-playing-surface-analytics/PlayList.csv\")\n\n# Phase I: Data Merge - Most of the data needed is on the above named datasets. However, there are some holes, specifically\n# in the PlayKey column. To fix it, I merged the files on the PlayKey column and then again on the GameID column to add the\n# data to the set where PlayKey was null\n\nresult = pd.merge(injury, \n                 playlist,\n                 on= 'PlayKey',\n                 how ='left')\n\nresults =result.drop(['PlayerKey_y', 'GameID_y'],axis=1)\nresults.rename(columns={'PlayerKey_x':'PlayerKey', 'GameID_x':'GameID'}, inplace=True)\n\n# After the first merge, all the other data can be based around the GameID column since it contains both the Player and Game IDs\n\nplaylist2 = playlist.drop_duplicates(subset =\"GameID\", \n                     keep = 'first')\n\nresult2 = pd.merge(results, \n                 playlist2,\n                 on= 'GameID',\n                 how ='left')\n\n# This final piece eliminates the null data from the first merge and cleans up the column names\n\nresults_final =result2.drop(['PlayerKey_y', 'PlayKey_y', 'PlayerDay_x',\n                             'PlayerGame_x', 'StadiumType_x', 'FieldType_x',\n                             'Temperature_x','Weather_x', 'PlayType_y',\n                             'PlayerGamePlay_y', 'Position_x', 'PositionGroup_x', 'RosterPosition_x'],axis=1)\n\nresults_final.rename(columns={'PlayerKey_x': 'PlayerKey', 'PlayKey_x': 'PlayKey', 'PlayerDay_y': 'PlayerDay',\n                             'PlayerGame_y': 'PlayerGame', 'StadiumType_y': 'StadiumType', 'FieldType_y': 'FieldType',\n                             'Temperature_y': 'Temperature','Weather_y': 'Weather', 'PlayType_x': 'PlayType',\n                             'PlayerGamePlay_x':'PlayerGamePlay', 'Position_y': 'Position',\n                             'PositionGroup_y':'PositionGroup', 'RosterPosition_y':'RosterPosition'}, inplace=True)\nprint(results_final.head())\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Phase II: Data Clean-up - Two columns (StadiumType and Weather) do not have standard reporting system. Data in these two\n# columns will be reclassified to yield better interpretation of the data.\n\n# Weather Category: Cloudy\nresults_final['Weather'].replace('Partly Cloudy', 'Cloudy', inplace=True)\nresults_final['Weather'].replace('Coudy', 'Cloudy', inplace=True)\nresults_final['Weather'].replace('Party Cloudy', 'Cloudy', inplace=True)\nresults_final['Weather'].replace('Cloudy and Cool', 'Cloudy', inplace=True)\nresults_final['Weather'].replace('Cloudy, 50% change of rain', 'Cloudy', inplace=True)\nresults_final['Weather'].replace('Cloudy with periods of rain, thunder possible. Winds shifting to WNW, 10-20 mph.', 'Cloudy', inplace=True)\nresults_final['Weather'].replace('Mostly cloudy', 'Cloudy', inplace=True)\nresults_final['Weather'].replace('Sun & clouds', 'Cloudy', inplace=True)\n\n# Weather Category: Clear\nresults_final['Weather'].replace('Clear and warm', 'Clear', inplace=True)\nresults_final['Weather'].replace('Sunny', 'Clear', inplace=True)\nresults_final['Weather'].replace('Mostly sunny', 'Clear', inplace=True)\nresults_final['Weather'].replace('Mostly Sunny', 'Clear', inplace=True)\nresults_final['Weather'].replace('Sunny and clear', 'Clear', inplace=True)\nresults_final['Weather'].replace('Clear and Sunny', 'Clear', inplace=True)\nresults_final['Weather'].replace('Clear skies', 'Clear', inplace=True)\nresults_final['Weather'].replace('Clear Skies', 'Clear', inplace=True)\nresults_final['Weather'].replace('Clear and cold', 'Clear', inplace=True)\nresults_final['Weather'].replace('Cold', 'Clear', inplace=True)\nresults_final['Weather'].replace('Fair', 'Clear', inplace=True)\n\n# Weather Category: Rain\nresults_final['Weather'].replace('Rain shower', 'Rain', inplace=True)\nresults_final['Weather'].replace('Light Rain', 'Rain', inplace=True)\n\n# Weather Category: Indoors\nresults_final['Weather'].replace('Indoor', 'Indoors', inplace=True)\nresults_final['Weather'].replace('N/A (Indoors)', 'Indoors', inplace=True)\nresults_final['Weather'].replace('Controlled Climate', 'Indoors', inplace=True)\n\n# Weather Category: Unknown\nresults_final['Weather'].fillna('Unknown', inplace=True)\n\n# Stadium Category: Indoors\nresults_final['StadiumType'].replace('Indoor', 'Indoors', inplace=True)\nresults_final['StadiumType'].replace('Dome', 'Indoors', inplace=True)\nresults_final['StadiumType'].replace('Retr. Roof-Closed', 'Indoors', inplace=True)\nresults_final['StadiumType'].replace('Closed Dome', 'Indoors', inplace=True)\nresults_final['StadiumType'].replace('Domed, closed', 'Indoors', inplace=True)\nresults_final['StadiumType'].replace('Retr. Roof - Closed', 'Indoors', inplace=True)\nresults_final['StadiumType'].replace('Retractable Roof', 'Indoors', inplace=True)\nresults_final['StadiumType'].replace('Indoor, Roof Closed', 'Indoors', inplace=True)\n\n# Stadium Category: Outdoors\nresults_final['StadiumType'].replace('Outdoor', 'Outdoors', inplace=True)\nresults_final['StadiumType'].replace('Retr. Roof - Open', 'Outdoors', inplace=True)\nresults_final['StadiumType'].replace('Indoor, Open Roof', 'Outdoors', inplace=True)\nresults_final['StadiumType'].replace('Oudoor', 'Outdoors', inplace=True)\nresults_final['StadiumType'].replace('Outddors', 'Outdoors', inplace=True)\nresults_final['StadiumType'].replace('Open', 'Outdoors', inplace=True)\nresults_final['StadiumType'].replace('Heinz Field', 'Outdoors', inplace=True)\n\n# Stadium Category: Unknown\nresults_final['StadiumType'].fillna('Unknown', inplace=True)\n\n# Other Clean-up\nresults_final['Temperature'].replace(-999, 'Unknown', inplace=True)\nresults_final.fillna('Unknown', inplace=True)\n\nprint(results_final)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Phase III Data Analysis\n# Question 1: Do the characteristics of the stadium influence injuries?\n\ncolor = [\"#013369\", \"#D50A0A\", \"#95a5a6\", \"#e74c3c\", \"#34495e\", \"#2ecc71\"]\n\nplt.figure(figsize = (15,5), facecolor = \"white\",)\nplt.rcParams['font.size'] = 15\nplt.subplot(1, 2, 1,).title.set_text(\"Field Surface\")\nsns.set_style(\"whitegrid\")\nresults_final['Surface'].value_counts().plot.pie(autopct='%.1f%%', colors = ['#013369', '#D50A0A'],textprops=dict(color=\"w\", weight =\"bold\"), shadow=True, explode =(0.05, 0.05),)\nplt.legend(bbox_to_anchor=(0.87,0.2))\nplt.ylabel(\"Percentage of Injuries\")\n\nplt.subplot(1, 2, 2,).title.set_text(\"Stadium Type\")\nsns.set_style(\"whitegrid\")\nsns.countplot(x = 'StadiumType', hue='Surface', data=results_final, palette = sns.color_palette(color),)\nplt.ylabel(\"Number of Injuries\")\nplt.xlabel(\" \")\n\nplt.text(x = -3.3, y = 60.2, s = \"Do Stadium Characteristics Influence Injuries?\",\n               fontsize = 26, color = \"black\", weight = 'bold', alpha = .75)\nplt.text(x = -3.3, y = 56.9, \n               s = \"Comparison between Field Surfaces and Stadium Types on Overall Injuries \",\n              fontsize = 15, color = \"black\", alpha = .85)\nplt.text(x = -3.8, y = -10, s = 'Source: NFL 1st and Future - Playing Surface Analytics                            https://www.kaggle.com/c/NFL-playing-surface-analytics',fontsize = 14, color = 'white', backgroundcolor = 'gray')\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"color = [\"#013369\", \"#D50A0A\", \"#95a5a6\", \"#e74c3c\", \"#34495e\", \"#2ecc71\"]\ncolor2 = [\"#241075\", \"#BC9428\", \"#A5ACAF\", \"#66C010\", \"#4790DE\", \"#2ecc71\"]\n\nplt.figure(figsize = (15,5), facecolor = \"white\",)\nplt.rcParams['font.size'] = 15\nplt.subplot(1, 2, 1,).title.set_text(\"Player Position\")\nsns.set_style(\"whitegrid\")\nsns.countplot(x = 'RosterPosition', hue='Surface', data=results_final, palette = sns.color_palette(color),)\nplt.ylabel(\"Number of Injuries\")\nplt.legend(bbox_to_anchor=(0.55,0.5))\nplt.xticks(rotation='vertical')\nplt.xlabel(\" \")\n\nplt.subplot(1, 2, 2,).title.set_text(\"Injury Type\")\nsns.set_style(\"whitegrid\")\nsns.countplot(x ='BodyPart', hue='Surface', data=results_final,palette = sns.color_palette(color))\nplt.ylabel(\"Number of Injuries\")\nplt.legend(bbox_to_anchor=(0.82,0.6))\nplt.xticks(rotation=70)\nplt.xlabel(\" \")\n\nplt.text(x = -6.5, y = 32.2, s = \"Do Stadium Characteristics Influence Injuries?\",\n               fontsize = 26, color = \"black\", weight = 'bold', alpha = .75)\nplt.text(x = -6.5, y = 30.3, \n               s = \"Comparison between Field Surfaces on Player Position and Injury Type\",\n              fontsize = 15, color = \"black\", alpha = .85)\nplt.text(x = -6.5, y = -15, s = 'Source: NFL 1st and Future - Playing Surface Analytics                            https://www.kaggle.com/c/NFL-playing-surface-analytics',fontsize = 14, color = 'white', backgroundcolor = 'gray')\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"color = [\"#013369\", \"#D50A0A\", \"#95a5a6\", \"#e74c3c\", \"#34495e\", \"#2ecc71\"]\ncolor2 = [\"#241075\", \"#BC9428\", \"#A5ACAF\", \"#66C010\", \"#4790DE\", \"#2ecc71\"]\n\nplt.figure(figsize = (15,5), facecolor = \"white\",)\nplt.rcParams['font.size'] = 15\nplt.subplot(1, 1, 1,).title.set_text(\"Play Type\")\nsns.set_style(\"whitegrid\")\nsns.countplot(x = 'PlayType', hue='Surface', data=results_final, palette = sns.color_palette(color),)\nplt.ylabel(\"Number of Injuries\")\nplt.legend(bbox_to_anchor=(0.9,0.5))\nplt.xticks(rotation='vertical')\nplt.xlabel(\" \")\n\n\nplt.text(x = -.5, y = 22.2, s = \"Do Stadium Characteristics Influence Injuries?\",\n               fontsize = 26, color = \"black\", weight = 'bold', alpha = .75)\nplt.text(x = -.5, y = 20.8, \n               s = \"Comparison between Field Surfaces on Play Type\",\n              fontsize = 15, color = \"black\", alpha = .85)\nplt.text(x = -.5, y = -12, s = 'Source: NFL 1st and Future - Playing Surface Analytics                            https://www.kaggle.com/c/NFL-playing-surface-analytics',fontsize = 14, color = 'white', backgroundcolor = 'gray')\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"color = [\"#013369\", \"#D50A0A\", \"#95a5a6\", \"#e74c3c\", \"#34495e\", \"#2ecc71\"]\ncolor2 = [\"#241075\", \"#BC9428\", \"#A5ACAF\", \"#66C010\", \"#4790DE\", \"#2ecc71\"]\n\nplt.figure(figsize = (15,5), facecolor = \"white\",)\nplt.rcParams['font.size'] = 15\nplt.subplot(1, 1, 1,).title.set_text(\"NFL Season Week\")\nsns.set_style(\"whitegrid\")\nsns.countplot(x = 'PlayerGame', hue='Surface', data=results_final, palette = sns.color_palette(color),)\nplt.ylabel(\"Number of Injuries\")\nplt.legend(bbox_to_anchor=(0.8,0.5))\n\nplt.xlabel(\" \")\n\n\nplt.text(x = 0, y = 10, s = \"Do Stadium Characteristics Influence Injuries?\",\n               fontsize = 26, color = \"black\", weight = 'bold', alpha = .75)\nplt.text(x = 0, y = 9.5, \n               s = \"Comparison between Field Surfaces on Week of Injury\",\n              fontsize = 15, color = \"black\", alpha = .85)\nplt.text(x = 0, y = -1.5, s = 'Source: NFL 1st and Future - Playing Surface Analytics                            https://www.kaggle.com/c/NFL-playing-surface-analytics',fontsize = 14, color = 'white', backgroundcolor = 'gray')\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"color = [\"#013369\", \"#D50A0A\", \"#95a5a6\", \"#e74c3c\", \"#34495e\", \"#2ecc71\"]\ncolor2 = [\"#241075\", \"#BC9428\", \"#A5ACAF\", \"#66C010\", \"#4790DE\", \"#2ecc71\"]\ncolor3 = [\"#99213E\", \"#FFB700\", \"#000000\", \"#66C010\", \"#4790DE\", \"#2ecc71\"]\n\nplt.figure(figsize = (15,5), facecolor = \"white\",)\nplt.rcParams['font.size'] = 15\nplt.subplot(1, 1, 1,).title.set_text(\"Injury Type\")\nsns.set_style(\"whitegrid\")\nsns.countplot(x = 'StadiumType', hue='BodyPart', data=results_final, palette = sns.color_palette(color2),)\nplt.ylabel(\"Number of Injuries\")\nplt.legend(bbox_to_anchor=(0.8,0.8))\n\nplt.xlabel(\" \")\n\n\nplt.text(x = -0.5, y = 38, s = \"Do Stadium Characteristics Influence Injuries?\",\n               fontsize = 26, color = \"black\", weight = 'bold', alpha = .75)\nplt.text(x = -0.5, y = 36.2, \n               s = \"Comparison of Stadium Types on Type of Injury\",\n              fontsize = 15, color = \"black\", alpha = .85)\nplt.text(x = -0.5, y = -6.5, s = 'Source: NFL 1st and Future - Playing Surface Analytics                            https://www.kaggle.com/c/NFL-playing-surface-analytics',fontsize = 14, color = 'white', backgroundcolor = 'gray')\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Question 2: Do the characteristics of the player influence injuries?\n\ncolor = [\"#013369\", \"#D50A0A\", \"#95a5a6\", \"#e74c3c\", \"#34495e\", \"#2ecc71\"]\ncolor2 = [\"#241075\", \"#BC9428\", \"#A5ACAF\", \"#66C010\", \"#4790DE\", \"#2ecc71\"]\ncolor3 = [\"#99213E\", \"#FFB700\", \"#000000\", \"#66C010\", \"#4790DE\", \"#2ecc71\"]\n\nplt.figure(figsize = (15,5), facecolor = \"white\",)\nplt.rcParams['font.size'] = 15\nplt.subplot(1, 2, 1,).title.set_text(\"Stadium Type\")\nsns.set_style(\"whitegrid\")\nsns.countplot(x = 'RosterPosition', hue='StadiumType', data=results_final, palette = sns.color_palette(color3),)\nplt.ylabel(\"Number of Injuries\")\nplt.legend(bbox_to_anchor=(0.55,0.5))\nplt.xticks(rotation='vertical')\nplt.xlabel(\" \")\n\nplt.subplot(1, 2, 2,).title.set_text(\"Injury Type\")\nsns.set_style(\"whitegrid\")\nsns.countplot(x ='RosterPosition', hue='BodyPart', data=results_final,palette = sns.color_palette(color2))\nplt.ylabel(\"Number of Injuries\")\nplt.legend(bbox_to_anchor=(0.45,0.5))\nplt.xticks(rotation='vertical')\nplt.xlabel(\" \")\n\nplt.text(x = -9.5, y = 16.2, s = \"Do Player Characteristics Influence Injuries?\",\n               fontsize = 26, color = \"black\", weight = 'bold', alpha = .75)\nplt.text(x = -9.5, y = 15.3, \n               s = \"Comparison between Play Position on Stadium Types and Injury Types\",\n              fontsize = 15, color = \"black\", alpha = .85)\nplt.text(x = -9.6, y = -8, s = 'Source: NFL 1st and Future - Playing Surface Analytics                            https://www.kaggle.com/c/NFL-playing-surface-analytics',fontsize = 14, color = 'white', backgroundcolor = 'gray')\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Question 3: Do the characteristics of the weather influence injuries?\n\ncolor = [\"#013369\", \"#D50A0A\", \"#95a5a6\", \"#e74c3c\", \"#34495e\", \"#2ecc71\"]\ncolor2 = [\"#241075\", \"#BC9428\", \"#A5ACAF\", \"#66C010\", \"#4790DE\", \"#2ecc71\"]\ncolor3 = [\"#99213E\", \"#FFB700\", \"#000000\", \"#66C010\", \"#4790DE\", \"#2ecc71\"]\n\nplt.figure(figsize = (15,5), facecolor = \"white\",)\nplt.rcParams['font.size'] = 15\nplt.subplot(1, 1, 1,).title.set_text(\"Weather\")\nsns.set_style(\"whitegrid\")\nsns.countplot(x = 'RosterPosition', hue='Weather', data=results_final, palette = sns.color_palette(color3),)\nplt.ylabel(\"Number of Injuries\")\nplt.xticks(rotation='vertical')\nplt.legend(bbox_to_anchor=(0.85,0.5))\nplt.xlabel(\" \")\n\n\nplt.text(x = -0.5, y = 13.8, s = \"Do Weather Characteristics Influence Injuries?\",\n               fontsize = 26, color = \"black\", weight = 'bold', alpha = .75)\nplt.text(x = -0.5, y = 13.1, \n               s = \"Comparison of Weather Types on Player Positions\",\n              fontsize = 15, color = \"black\", alpha = .85)\nplt.text(x = -0.6, y = -7, s = 'Source: NFL 1st and Future - Playing Surface Analytics                            https://www.kaggle.com/c/NFL-playing-surface-analytics',fontsize = 14, color = 'white', backgroundcolor = 'gray')\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"color = [\"#013369\", \"#D50A0A\", \"#95a5a6\", \"#e74c3c\", \"#34495e\", \"#2ecc71\"]\ncolor2 = [\"#241075\", \"#BC9428\", \"#A5ACAF\", \"#66C010\", \"#4790DE\", \"#2ecc71\"]\ncolor3 = [\"#99213E\", \"#FFB700\", \"#000000\", \"#66C010\", \"#4790DE\", \"#2ecc71\"]\n\nplt.figure(figsize = (15,5), facecolor = \"white\",)\nplt.rcParams['font.size'] = 15\nplt.subplot(1, 1, 1,).title.set_text(\"Weather\")\nsns.set_style(\"whitegrid\")\nsns.countplot(x = 'BodyPart', hue='Weather', data=results_final, palette = sns.color_palette(color3),)\nplt.ylabel(\"Number of Injuries\")\n\nplt.legend(bbox_to_anchor=(0.85,0.8))\nplt.xlabel(\" \")\n\n\nplt.text(x = -0.5, y = 23.8, s = \"Do Weather Characteristics Influence Injuries?\",\n               fontsize = 26, color = \"black\", weight = 'bold', alpha = .75)\nplt.text(x = -0.5, y = 22.6, \n               s = \"Comparison of Weather Types on Injury Types\",\n              fontsize = 15, color = \"black\", alpha = .85)\nplt.text(x = -0.6, y = -4, s = 'Source: NFL 1st and Future - Playing Surface Analytics                            https://www.kaggle.com/c/NFL-playing-surface-analytics',fontsize = 14, color = 'white', backgroundcolor = 'gray')\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(playlist['StadiumType'].unique())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(playlist['Weather'].unique())","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":1}