{"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_type":"markdown","source":"# Note regarding this analysis:\n\n**Welcome to my Notebook!**\n**I'm just getting started in Data Analysis & working with Dataframes. Therefore my target is not to make a full analysis, but getting comfortable with Dataframes & data visualisation**\n\n**I'm thankful for any helpful comments and input, so that I can improve my skill.**"},{"metadata":{},"cell_type":"markdown","source":"# My imported packages"},{"metadata":{"trusted":true},"cell_type":"code","source":"import seaborn as sns\nimport matplotlib.pyplot as plt\nimport plotly.express as px\nfrom matplotlib.pyplot import figure\nimport statistics","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Inspection of 1st Dataset (InjuryRecord)"},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"injuryrec = pd.read_csv('/kaggle/input/nfl-playing-surface-analytics/InjuryRecord.csv')\ninjuryrec.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Visualisation of the injured Body Parts"},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"plt.figure(figsize=(8,8))\ninjuries = sns.countplot(x = 'BodyPart', data = injuryrec)\nplt.title('Injured Body Parts')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**The most injuries occur in Knees and Ankles. Heel injuries have the least part in the over all injuries**\n\nHow are the injuries distributed when we look at the Surface?"},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"# plot of FieldTyp per Indoor & Outdoor Game \nplt.figure(figsize=(15,5))\nsns.countplot(x = 'BodyPart', data = injuryrec, hue = 'Surface')\nplt.title('Injured Body Parts by Surfaces')\nplt.xlabel('Injured Body Parts')\nplt.ylabel('Number of Injuries')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"There is no difference in the number of knee injuries on different surfaces. \nThere are more ankle and toe injuries when played on synthetic surface.\nGeneral food injuries seem to occur more often on natural turf.\n\nHow long are players ,,out of the game\" with those injuries?"},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"l = [injuryrec['DM_M1'].count()]  # list of the player unjured per time point\nl.append(injuryrec['DM_M7'].value_counts()[1])\nl.append(injuryrec['DM_M28'].value_counts()[1])\nl.append(injuryrec['DM_M42'].value_counts()[1])\n\n\nl2 = []\nfor i in range(0, len(l)-1):\n        l2.append(l[i] - l[i+1])\nl2.append(injuryrec['DM_M42'].value_counts()[1])\n#-----------------------------------------------------for the graphic -----------------------------------------------------\nl3 = ['1 day', '7 days', '28 days', '42 days'] # label\nplt.figure(figsize=(10,5))\nplt.title('How many players are unable to play for how long?') #title\nplt.ylabel('Numbers of players')\nplt.bar(l3, l2)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------**\n# 2nd Dataset (PlayList)"},{"metadata":{},"cell_type":"markdown","source":"loading the dataset"},{"metadata":{"trusted":true,"_kg_hide-input":true,"_kg_hide-output":true},"cell_type":"code","source":"playlist = pd.read_csv('/kaggle/input/nfl-playing-surface-analytics/PlayList.csv')\nplaylist.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"plt.figure(figsize=(18,5))\nsns.countplot(x = 'Temperature', data = playlist)\nplt.xlabel('Temperature in °F')\nplt.ylabel('Number of Games')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"There is a large amount of ,,nonsese\" values, saying there were -999°F (-572°C) in the stadium.\n- Not sure how to proceed with this values. \n    - change them to zero is not possible because in history there were games where the temperature was below 0°F!\n    - dropping them would mean huge data loss!\n        \n- a reasonable value would be -9°F but can't be sure that this is the true value and therefore would corrupt the data\n\n**I left this problem up to now because I could not find a good solution**"},{"metadata":{},"cell_type":"markdown","source":"# In what kind of Stadiums did the games take place?"},{"metadata":{},"cell_type":"markdown","source":"When we look at the content of the 'StadiumType' column we see that there are a lot of redundant categories.\nSome with spelling mistakes (Ourdoor) or variances of the same category ( Outdoor & Outdoors)\nThis has to be cleaned.\nThere are some strange categories like: Dome, Domed, Retractable Roof, Bowl and Cloudy which don't give much insight into the StadiumType or the status of Indoor or Outdoor."},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"#stadium = playlist.groupby('StadiumType').count()\nplt.figure(figsize=(10,10))\nsns.countplot(y = 'StadiumType', data = playlist) # the plot is better visible when turned! \nplt.xlabel('Number of Games')\nplt.ylabel('Types of Stadium')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"* It's clear that the 'StadiumType' category has to be cleaned thoroughly\n* there are redundant categories and misspelling.... (Outddors, Oudoor) which have to be corrected & combined\n* what does ,,Open\" mean? (Open Dome maybe?)\n\n**most of the games were played outdoor**"},{"metadata":{},"cell_type":"markdown","source":"I simplified the categories of the StadiumType Column by exchanging the Data"},{"metadata":{"trusted":true},"cell_type":"code","source":"# cleaning the StadiumType column:\nd = {'Oudoor': 'Outdoor', 'Outdoors': 'Outdoor', 'Ourdoor': 'Outdoor', 'Outdor': 'Outdoor', 'Outside': 'Outdoor', 'Indoors' : 'Indoor', 'Outddors' : 'Outdoor', \\\n     'Domed, closed': 'Closed Dome', 'Domed': 'Dome', 'Domed, Open':'Open Dome', 'Dome, closed' : 'Closed Dome', 'Retr. Roof-Closed' : 'Retr. Roof Closed',\\\n     'Retr. Roof - Closed': 'Retr. Roof Closed', 'Outdoor Retr Roof-Open': 'Retr. Roof-Open', 'Indoor, Open Roof' : 'Retr. Roof-Open', 'Indoor, Roof Closed': 'Retr. Roof Closed',\\\n     'Retr. Roof - Open': 'Retr. Roof-Open','Heinz Field' : 'Outdoor'}\nplaylist_mod = playlist.replace(d)\n\nplaylist_mod['StadiumType'].unique() #checking for unique categories","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"plt.figure(figsize=(10,10))\nsns.countplot(y = 'StadiumType', data = playlist_mod) # the plot is better visible when turned! \nplt.xlabel('Number of Games')\nplt.ylabel('Types of Stadium')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"I cleaned the category as careful as possible. \nIn my opinion the two categories 'Bowl' & 'Cloudy' could be droped when there is a focus on StadiumType and FieldType because they do not contribute to the analysis here.\n\nIn the following I combined all the StadiumTypes to two categories: ,,Indoor\" & \"Outdoor\":"},{"metadata":{"trusted":true},"cell_type":"code","source":"d2 = {'Open' : 'Outdoor', 'Closed Dome' : 'Indoor', 'Dome' : 'Indoor', 'Retr. Roof Closed' : 'Indoor', 'Retr. Roof-Open' : 'Outdoor', 'Retractable Roof' : 'Indoor', 'Open Dome' : 'Outdoor', 'Domed, open' : 'Outdoor' }\nplaylist_mod2 = playlist_mod.replace(d2)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"And I droped ,,Bowl\" & \"Cloudy\" from the StadiumType column"},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"# drop lines with a certain content from a column\nindexNames = playlist_mod2[playlist_mod2['StadiumType'] == 'Bowl'].index\nplaylist_mod2.drop(indexNames, inplace = True)\nindexNames = playlist_mod2[playlist_mod2['StadiumType'] == 'Cloudy'].index\nplaylist_mod2.drop(indexNames, inplace = True)\nplaylist_mod2['StadiumType'].unique()","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(8,8))\nsns.countplot(x = 'StadiumType', data = playlist_mod2) # the plot is better visible when turned! \nplt.ylabel('Number of Games')\nplt.xlabel('Types of Stadium')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**There are in general more games played outdoor than indoor**"},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"# plot of FieldTyp per Indoor & Outdoor Game \nplt.figure(figsize=(20,8))\nsns.countplot(x = 'StadiumType', data = playlist_mod2, hue = 'FieldType')\nplt.title('Distribution of Synthetic and Natural turf in games played indoor or outdoor')\nplt.xlabel('Field types')\nplt.ylabel('Number of Games')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"** Outdoor games are played more on natural turf that synthetic one. Indoor games however are played on a synthetic field**"},{"metadata":{},"cell_type":"markdown","source":""},{"metadata":{},"cell_type":"markdown","source":"# How is generally the weather during the recorded games?"},{"metadata":{},"cell_type":"markdown","source":"The weather and the surface together could have an impact on the injuries. For example could rain make the natural surface more slippery which could lead to more injuries."},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"plt.figure(figsize=(15,15))\nsns.countplot(y = 'Weather', data = playlist) # the plot is better visible when turned! \nplt.title('Recorded weather categories ')\nplt.xlabel('Number of Games')\nplt.ylabel('Weather conditions')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"* same which holds true for the 'Weather' Category\n    * spelling mistakes, redundant categories (Clear and Sunny & Clear and sunny)\n    * the same ,,cleaning of categories\" should be done, but could lead do corruption of the data.\n    \n** during most of the games the weather was cloudy **\n\n\nLet's have a look at the players positions:"},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(20,5))\nsns.countplot(x = 'RosterPosition', data = playlist) # the plot is better visible when turned! \nplt.title('Player Positions')\nplt.xlabel('Positions')\nplt.ylabel('Number of records')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Merging Dataframes"},{"metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"trusted":true},"cell_type":"code","source":"injuryrec.head()","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"trusted":true},"cell_type":"code","source":"playlist_mod.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"inplay = pd.merge(injuryrec, playlist_mod, on = 'PlayKey')\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"inplay.info()","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"inplay.tail()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**After merging the Dataframes we can look more deeply into the conections of Injuries and the other categories**"},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(15,5))\nsns.countplot(x = 'PlayType', data = inplay, hue = 'BodyPart') \nplt.title('Injuries per Play Type')\nplt.xlabel('Play Type')\nplt.ylabel('Number of injuries')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"The most knee injuries occur during Pass plays and the most Ankle injuries during Passes & Rushes. But in general Knee and Ankle injuries are occuring throughout all the plays exept for 'Punt Not Returned'. Plays with high amount of movement like passes and rushes seem to cause more injuries than static plays as 'Punt Not Returned'."},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(15,5))\nsns.countplot(x = 'PositionGroup', data = inplay, hue = 'BodyPart') \nplt.title('Injuries per Position Group')\nplt.xlabel('Position')\nplt.ylabel('Number of injuries')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Linebackers, defensive backs, wide reciever and running backs have high values of knee and ankle injuries. In general the offensive line shows more ankle injuries and the defensive line more Knee injuries.\n\nBut we can see that quaterbacks are not mentioned in this plot. So they seem to have few injuries of the lower body region.\n\nWe can go more into detail of the injuries of the different players position:"},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(15,8))\nsns.countplot(x = 'Position', data = inplay, hue = 'BodyPart') \nplt.title('Injuries per Position Group')\nplt.xlabel('Position')\nplt.ylabel('Number of injuries')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Outside Linebackers (OLB) are part of the defense line and as already seen in the previous plot they show twice as much knee injuries than ankle injuries. This might be due to their static position in defending pass play or running play. Same can be seen for an other position of the defensive line like Inside linebackers (ILB).\n\nWide recievers are running down the field as part of the offensive team and show knee injuries as well as ankle injuries in nearly the same amounts. They are fast sprinters so who accalerate fastly and stopp abruptly. \n\nIn general OLB & WR ,,get hurt\" according to this data. "},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(15,5))\nsns.countplot(x = 'PositionGroup', data = inplay, hue = 'Surface') \nplt.title('Injuries per Position Group')\nplt.xlabel('Position Group')\nplt.ylabel('Number of injuries')\nplt.figure(figsize=(15,5))\nsns.countplot(x = 'Position', data = inplay, hue = 'Surface') \nplt.title('Injuries per Position')\nplt.xlabel('Position')\nplt.ylabel('Number of injuries')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"In general D-line & O-line show the majority of injuries on a natural surface (upper plot). \nTight ends however injur themself equally no matter what the surface is. :D \nFor OLB the most injuries occur on natural turf whereas for ther wide recievers most of the injuies occur on a synthetic surface. For corner backs (CB) the majority of injuries occur on synthetic surfaces.\n\nWhen we look under which weather conditions this injuries occur:  \n\n**NOTE: I did not clear redundant categories of the weather column!**"},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(15,6))\nsns.countplot(x = 'BodyPart', data = inplay, hue = 'Weather') \nplt.title('Injuried Body Part')\nplt.xlabel('Body Part')\nplt.ylabel('Number of injuries')\nplt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.) # putting the legend outside of the figure\nplt.figure(figsize=(15,10))\nsns.countplot(x = 'BodyPart', data = inplay, hue = 'Temperature') \nplt.title('Injuried Body Part by Temperature')\nplt.xlabel('Body Part')\nplt.ylabel('Number of injuries')\nplt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.) # putting the legend outside of the figure\nplt.figure(figsize=(15,10))\nsns.countplot(x = 'Surface', data = inplay, hue = 'Temperature') \nplt.title('Surface by Temperature')\nplt.xlabel('Surface')\nplt.ylabel('Number of injuries')\nplt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.) # putting the legend outside of the figure\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Injuries occur on synthetic surface throughout the whole recorded temperature range. But mostly around 65°F. On natural turf however there are more injuries at higher temperatures. The Knee injuries occur mostly at temperatures of 65°F and higher and during cloudy, sunny or partially sunny weather, whereas ankle injuries are occuring during all kind of temperatures but as well during cloudy, sunny or partially sunny weather. "},{"metadata":{},"cell_type":"markdown","source":"**-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n**\n# 3rd Dataset (player)"},{"metadata":{"trusted":true},"cell_type":"code","source":"player = pd.read_csv('/kaggle/input/nfl-playing-surface-analytics/PlayerTrackData.csv')\nplayer.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"total = pd.merge(inplay, player, on = 'PlayKey')\ntotal.head()","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"fig = px.scatter_matrix(total,\n    dimensions=['Surface', 's'],\n    color=\"BodyPart\")\nfig.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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}