{"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":"### Hi guys! This notebook will slowly enter into competition and achieve high scores.\n\n#### Please support and stay updated.\n\n### This notebook holds analysis and visualization for Players, Games, Plays, PffScouting data. To view analysis and visualizations of year wise tracking data check this notebook -> [NFL yearly tracking data complete !! 🏈](https://www.kaggle.com/zwartfreak/nfl-yearly-tracking-data-complete)\n\n##### P.S. This notebook is not completed yet, building it daily.","metadata":{}},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\nplayers = pd.read_csv('../input/nfl-big-data-bowl-2022/players.csv')\ngames = pd.read_csv('../input/nfl-big-data-bowl-2022/games.csv')\nplays = pd.read_csv('../input/nfl-big-data-bowl-2022/plays.csv')\npffscouting = pd.read_csv('../input/nfl-big-data-bowl-2022/PFFScoutingData.csv')\n#tracking2018 = pd.read_csv('../input/nfl-big-data-bowl-2022/tracking2018.csv')\n#tracking2019 = pd.read_csv('../input/nfl-big-data-bowl-2022/tracking2019.csv')\n#tracking2020 = pd.read_csv('../input/nfl-big-data-bowl-2022/tracking2020.csv')","metadata":{"execution":{"iopub.status.busy":"2021-12-29T06:38:12.650680Z","iopub.execute_input":"2021-12-29T06:38:12.651082Z","iopub.status.idle":"2021-12-29T06:38:12.931209Z","shell.execute_reply.started":"2021-12-29T06:38:12.650984Z","shell.execute_reply":"2021-12-29T06:38:12.930441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> ## Data analysis","metadata":{}},{"cell_type":"code","source":"players.shape, games.shape, plays.shape","metadata":{"execution":{"iopub.status.busy":"2021-12-29T06:38:12.932869Z","iopub.execute_input":"2021-12-29T06:38:12.933113Z","iopub.status.idle":"2021-12-29T06:38:12.941097Z","shell.execute_reply.started":"2021-12-29T06:38:12.933083Z","shell.execute_reply":"2021-12-29T06:38:12.940364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### There are different datasets so we will analyse them one by one.","metadata":{}},{"cell_type":"markdown","source":"### 1. Players","metadata":{}},{"cell_type":"code","source":"players.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-29T06:38:12.942797Z","iopub.execute_input":"2021-12-29T06:38:12.943107Z","iopub.status.idle":"2021-12-29T06:38:12.965147Z","shell.execute_reply.started":"2021-12-29T06:38:12.943067Z","shell.execute_reply":"2021-12-29T06:38:12.964197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"players.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2021-12-29T06:38:12.968191Z","iopub.execute_input":"2021-12-29T06:38:12.968483Z","iopub.status.idle":"2021-12-29T06:38:12.979489Z","shell.execute_reply.started":"2021-12-29T06:38:12.968443Z","shell.execute_reply":"2021-12-29T06:38:12.978739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##### Let's drop the player's data whose 'birthDate' is not avalable.\n##### Also, let's drop the missing data from 'collegeName'.","metadata":{}},{"cell_type":"code","source":"players.dropna(subset=['collegeName', 'birthDate'], inplace=True, how='any')\nplayers = players.reset_index(drop=True)\nplayers.isnull().sum(), players.shape","metadata":{"execution":{"iopub.status.busy":"2021-12-29T06:38:12.980579Z","iopub.execute_input":"2021-12-29T06:38:12.980779Z","iopub.status.idle":"2021-12-29T06:38:12.998798Z","shell.execute_reply.started":"2021-12-29T06:38:12.980755Z","shell.execute_reply":"2021-12-29T06:38:12.997994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##### So players dataset is free of null values","metadata":{}},{"cell_type":"markdown","source":"#### Let's convert 'height' to a simple integer and 'birthDate' to 'Age'","metadata":{}},{"cell_type":"code","source":"for i in range(0,(len(players.height)-1)):\n    l = len(players.height[i])\n    if '-' in players.height[i]:\n        if l ==3:\n            players.height[i] = int(players.height[i][0])*12 + int(players.height[i][l-1])\n        else:\n            players.height[i] = int(players.height[i][0])*12 + int(players.height[i][l-1]) + 10\n    else:\n        players.height[i] = int(players.height[i])  \n        \n#players.height = players.height.astype('int')","metadata":{"execution":{"iopub.status.busy":"2021-12-29T06:38:13.000320Z","iopub.execute_input":"2021-12-29T06:38:13.000731Z","iopub.status.idle":"2021-12-29T06:38:13.920545Z","shell.execute_reply.started":"2021-12-29T06:38:13.000701Z","shell.execute_reply":"2021-12-29T06:38:13.919765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"players.height.unique()","metadata":{"execution":{"iopub.status.busy":"2021-12-29T06:38:13.921855Z","iopub.execute_input":"2021-12-29T06:38:13.922073Z","iopub.status.idle":"2021-12-29T06:38:13.929047Z","shell.execute_reply.started":"2021-12-29T06:38:13.922047Z","shell.execute_reply":"2021-12-29T06:38:13.928164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##### There is still a value which is not getting converted in to simple integer.","metadata":{}},{"cell_type":"code","source":"from datetime import datetime, date\ntoday = date.today()\n\nplayers.birthDate = pd.to_datetime(players.birthDate)\nplayers.birthDate = players.birthDate.dt.strftime(\"%Y/%m/%d\")\n\nfor j in range(0,(len(players.height)-1)):\n    born = datetime.strptime(str(players.birthDate[j]), \"%Y/%m/%d\").date()\n    players.birthDate[j] = today.year - born.year - ((today.month, today.day) < (born.month, born.day))\n\nplayers['age'] = players.birthDate\nplayers.drop(['birthDate'], axis=1, inplace=True)\n\n#players.birthDate = players.birthDate.astype('int')","metadata":{"execution":{"iopub.status.busy":"2021-12-29T06:38:13.930696Z","iopub.execute_input":"2021-12-29T06:38:13.930990Z","iopub.status.idle":"2021-12-29T06:38:15.088463Z","shell.execute_reply.started":"2021-12-29T06:38:13.930940Z","shell.execute_reply":"2021-12-29T06:38:15.087557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"players.age.unique()","metadata":{"execution":{"iopub.status.busy":"2021-12-29T06:38:15.089578Z","iopub.execute_input":"2021-12-29T06:38:15.089796Z","iopub.status.idle":"2021-12-29T06:38:15.098926Z","shell.execute_reply.started":"2021-12-29T06:38:15.089769Z","shell.execute_reply":"2021-12-29T06:38:15.098088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##### There is still a value which is not getting converted in to simple age just like above.","metadata":{}},{"cell_type":"code","source":"players.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-29T06:38:15.102195Z","iopub.execute_input":"2021-12-29T06:38:15.102709Z","iopub.status.idle":"2021-12-29T06:38:15.116017Z","shell.execute_reply.started":"2021-12-29T06:38:15.102675Z","shell.execute_reply":"2021-12-29T06:38:15.114935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> ## Wow that was quite a task, relaxed to do it.","metadata":{}},{"cell_type":"code","source":"players.dtypes","metadata":{"execution":{"iopub.status.busy":"2021-12-29T06:38:15.117204Z","iopub.execute_input":"2021-12-29T06:38:15.117422Z","iopub.status.idle":"2021-12-29T06:38:15.123730Z","shell.execute_reply.started":"2021-12-29T06:38:15.117395Z","shell.execute_reply":"2021-12-29T06:38:15.123183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##### Some are int and other are object, we will treat them later as required","metadata":{}},{"cell_type":"markdown","source":"### 2. Games","metadata":{}},{"cell_type":"code","source":"games.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-29T06:38:15.124921Z","iopub.execute_input":"2021-12-29T06:38:15.125374Z","iopub.status.idle":"2021-12-29T06:38:15.143564Z","shell.execute_reply.started":"2021-12-29T06:38:15.125344Z","shell.execute_reply":"2021-12-29T06:38:15.142458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"games.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2021-12-29T06:38:15.144772Z","iopub.execute_input":"2021-12-29T06:38:15.145022Z","iopub.status.idle":"2021-12-29T06:38:15.153849Z","shell.execute_reply.started":"2021-12-29T06:38:15.144984Z","shell.execute_reply":"2021-12-29T06:38:15.152958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##### No null values in Games","metadata":{}},{"cell_type":"markdown","source":"### 3. Plays","metadata":{}},{"cell_type":"code","source":"plays.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-29T06:38:15.155048Z","iopub.execute_input":"2021-12-29T06:38:15.155407Z","iopub.status.idle":"2021-12-29T06:38:15.189843Z","shell.execute_reply.started":"2021-12-29T06:38:15.155377Z","shell.execute_reply":"2021-12-29T06:38:15.189234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plays.isnull().sum().sum()","metadata":{"execution":{"iopub.status.busy":"2021-12-29T06:38:15.190896Z","iopub.execute_input":"2021-12-29T06:38:15.191631Z","iopub.status.idle":"2021-12-29T06:38:15.223182Z","shell.execute_reply.started":"2021-12-29T06:38:15.191590Z","shell.execute_reply":"2021-12-29T06:38:15.222354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##### That's hell lot of NULL values","metadata":{}},{"cell_type":"code","source":"plays.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2021-12-29T06:38:15.224631Z","iopub.execute_input":"2021-12-29T06:38:15.224962Z","iopub.status.idle":"2021-12-29T06:38:15.254271Z","shell.execute_reply.started":"2021-12-29T06:38:15.224934Z","shell.execute_reply":"2021-12-29T06:38:15.253375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##### As most of the values are null in 'kickBlockerId', 'penaltyCodes', 'penaltyJerseyNumbers', 'penaltyYards', 'passResult', 'returnerId'","metadata":{}},{"cell_type":"code","source":"plays.drop(columns=['kickBlockerId', 'penaltyCodes', 'penaltyJerseyNumbers', 'penaltyYards', 'passResult', 'returnerId', 'yardlineSide'], inplace=True)","metadata":{"execution":{"iopub.status.busy":"2021-12-29T06:38:15.255770Z","iopub.execute_input":"2021-12-29T06:38:15.256268Z","iopub.status.idle":"2021-12-29T06:38:15.267287Z","shell.execute_reply.started":"2021-12-29T06:38:15.256120Z","shell.execute_reply":"2021-12-29T06:38:15.264741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plays.kickReturnYardage.fillna(round(plays.kickReturnYardage.mean()), inplace=True)\nplays.kickLength.fillna(round(plays.kickLength.mean()), inplace=True)\nplays.isnull().sum().sum(), round(plays.kickReturnYardage.mean()), round(plays.kickLength.mean())\n\nplays.dropna(subset=['kickerId'],inplace=True)","metadata":{"execution":{"iopub.status.busy":"2021-12-29T06:38:15.268788Z","iopub.execute_input":"2021-12-29T06:38:15.269112Z","iopub.status.idle":"2021-12-29T06:38:15.297132Z","shell.execute_reply.started":"2021-12-29T06:38:15.269068Z","shell.execute_reply":"2021-12-29T06:38:15.296345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plays.isnull().sum().sum()","metadata":{"execution":{"iopub.status.busy":"2021-12-29T06:38:15.298261Z","iopub.execute_input":"2021-12-29T06:38:15.298561Z","iopub.status.idle":"2021-12-29T06:38:15.318657Z","shell.execute_reply.started":"2021-12-29T06:38:15.298531Z","shell.execute_reply":"2021-12-29T06:38:15.318050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##### So plays dataset is free of null values","metadata":{}},{"cell_type":"code","source":"players.columns, games.columns, plays.columns","metadata":{"execution":{"iopub.status.busy":"2021-12-29T06:38:15.319910Z","iopub.execute_input":"2021-12-29T06:38:15.320288Z","iopub.status.idle":"2021-12-29T06:38:15.328750Z","shell.execute_reply.started":"2021-12-29T06:38:15.320245Z","shell.execute_reply":"2021-12-29T06:38:15.328026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##### We can join games and plays dataset as gameId is the common column between them.","metadata":{}},{"cell_type":"code","source":"games_plays = pd.merge(games, plays, on='gameId')\ngames_plays.dtypes","metadata":{"execution":{"iopub.status.busy":"2021-12-29T06:38:15.329852Z","iopub.execute_input":"2021-12-29T06:38:15.330580Z","iopub.status.idle":"2021-12-29T06:38:15.366172Z","shell.execute_reply.started":"2021-12-29T06:38:15.330534Z","shell.execute_reply":"2021-12-29T06:38:15.365558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##### Different types of columns are there, we will treat them later as required","metadata":{}},{"cell_type":"code","source":"games_plays.columns","metadata":{"execution":{"iopub.status.busy":"2021-12-29T06:38:15.369381Z","iopub.execute_input":"2021-12-29T06:38:15.369854Z","iopub.status.idle":"2021-12-29T06:38:15.376371Z","shell.execute_reply.started":"2021-12-29T06:38:15.369819Z","shell.execute_reply":"2021-12-29T06:38:15.375471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"players.columns","metadata":{"execution":{"iopub.status.busy":"2021-12-29T06:38:15.377838Z","iopub.execute_input":"2021-12-29T06:38:15.378617Z","iopub.status.idle":"2021-12-29T06:38:15.388868Z","shell.execute_reply.started":"2021-12-29T06:38:15.378572Z","shell.execute_reply":"2021-12-29T06:38:15.388182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> ## Data visualization","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2021-12-29T06:38:15.390175Z","iopub.execute_input":"2021-12-29T06:38:15.390592Z","iopub.status.idle":"2021-12-29T06:38:16.363529Z","shell.execute_reply.started":"2021-12-29T06:38:15.390556Z","shell.execute_reply":"2021-12-29T06:38:16.362311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##### We can answer so many questions using Visualization.\n\n#### Q1. Which 'season' has most number of games?","metadata":{}},{"cell_type":"code","source":"games_plays.season.value_counts().plot(kind='barh', figsize=(10,6), color='yellow')\nplt.show()\n\n#You can just do it with games_plays.season.value_counts() this also but visualization feels better.","metadata":{"execution":{"iopub.status.busy":"2021-12-29T06:38:16.365085Z","iopub.execute_input":"2021-12-29T06:38:16.365679Z","iopub.status.idle":"2021-12-29T06:38:16.589367Z","shell.execute_reply.started":"2021-12-29T06:38:16.365635Z","shell.execute_reply":"2021-12-29T06:38:16.588370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Q2. Find young and tall players.\nAs we have converted above, we can use age and height here.","metadata":{}},{"cell_type":"code","source":"plt.subplot(1,2,1)\nplayers.height.value_counts().plot(kind='bar', figsize=(14,6), color='green')\nplt.xlabel('height')\n\nplt.subplot(1,2,2)\nplayers.age.value_counts().plot(kind='bar', figsize=(14,6), color='orange')\nplt.xlabel('age')\n\nplt.plot()","metadata":{"execution":{"iopub.status.busy":"2021-12-29T06:38:16.590554Z","iopub.execute_input":"2021-12-29T06:38:16.590764Z","iopub.status.idle":"2021-12-29T06:38:17.236003Z","shell.execute_reply.started":"2021-12-29T06:38:16.590737Z","shell.execute_reply":"2021-12-29T06:38:17.235210Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Q3. Which college has produced maximum number of players?","metadata":{}},{"cell_type":"code","source":"players.collegeName.nunique()","metadata":{"execution":{"iopub.status.busy":"2021-12-29T06:38:17.237520Z","iopub.execute_input":"2021-12-29T06:38:17.237984Z","iopub.status.idle":"2021-12-29T06:38:17.244310Z","shell.execute_reply.started":"2021-12-29T06:38:17.237939Z","shell.execute_reply":"2021-12-29T06:38:17.243514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##### As too many values unique values are there, let's not plot it.","metadata":{}},{"cell_type":"code","source":"players.collegeName.value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-12-29T06:38:17.247305Z","iopub.execute_input":"2021-12-29T06:38:17.247780Z","iopub.status.idle":"2021-12-29T06:38:17.258096Z","shell.execute_reply.started":"2021-12-29T06:38:17.247741Z","shell.execute_reply":"2021-12-29T06:38:17.257400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Q4. What is the relation between 'kicklength' and 'yardlinenumber'?","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}