{"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":"# 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\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 read-only \"../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# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-01-06T22:32:32.516924Z","iopub.execute_input":"2022-01-06T22:32:32.517351Z","iopub.status.idle":"2022-01-06T22:32:32.554433Z","shell.execute_reply.started":"2022-01-06T22:32:32.517238Z","shell.execute_reply":"2022-01-06T22:32:32.553401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib as mpl\nimport os, gc, re, warnings\nimport matplotlib.pyplot as plt\n\nimport matplotlib.patches as patches\nfrom matplotlib.patches import Arc\nfrom matplotlib import pyplot as plt\nimport matplotlib.patches as mpatches\n\nplt.style.use('fivethirtyeight')","metadata":{"execution":{"iopub.status.busy":"2022-01-06T22:32:32.557107Z","iopub.execute_input":"2022-01-06T22:32:32.557793Z","iopub.status.idle":"2022-01-06T22:32:33.665603Z","shell.execute_reply.started":"2022-01-06T22:32:32.557739Z","shell.execute_reply":"2022-01-06T22:32:33.664661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Important Library is called for operation.","metadata":{}},{"cell_type":"markdown","source":"#                                WORKING WITH GAME","metadata":{}},{"cell_type":"code","source":"df1 = pd.read_csv('../input/nfl-big-data-bowl-2022/games.csv')\ndf1","metadata":{"execution":{"iopub.status.busy":"2022-01-06T22:32:33.666972Z","iopub.execute_input":"2022-01-06T22:32:33.667218Z","iopub.status.idle":"2022-01-06T22:32:33.704668Z","shell.execute_reply.started":"2022-01-06T22:32:33.667191Z","shell.execute_reply":"2022-01-06T22:32:33.703739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1.info()","metadata":{"execution":{"iopub.status.busy":"2022-01-06T22:32:33.706529Z","iopub.execute_input":"2022-01-06T22:32:33.706899Z","iopub.status.idle":"2022-01-06T22:32:33.732259Z","shell.execute_reply.started":"2022-01-06T22:32:33.706829Z","shell.execute_reply":"2022-01-06T22:32:33.731012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1.describe()","metadata":{"execution":{"iopub.status.busy":"2022-01-06T22:32:33.736185Z","iopub.execute_input":"2022-01-06T22:32:33.736540Z","iopub.status.idle":"2022-01-06T22:32:33.764371Z","shell.execute_reply.started":"2022-01-06T22:32:33.736488Z","shell.execute_reply":"2022-01-06T22:32:33.763705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize=(12,6))\nsns.countplot(x=df1['season'], hue=df1['week'])\nplt.title('Game count per Season');","metadata":{"execution":{"iopub.status.busy":"2022-01-06T22:32:33.765360Z","iopub.execute_input":"2022-01-06T22:32:33.765744Z","iopub.status.idle":"2022-01-06T22:32:34.366051Z","shell.execute_reply.started":"2022-01-06T22:32:33.765712Z","shell.execute_reply":"2022-01-06T22:32:34.365061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"This graph shows the number of game per season.","metadata":{}},{"cell_type":"code","source":"fig = plt.figure(figsize=(12,6))\nhome = df1['homeTeamAbbr'].value_counts()\nsns.barplot(x=home.index, y=home.values, ci=None)\nplt.xlabel(\"Host Team\")\nplt.ylabel(\"Total\")\nplt.xticks(rotation=90);","metadata":{"execution":{"iopub.status.busy":"2022-01-06T22:32:34.368032Z","iopub.execute_input":"2022-01-06T22:32:34.368315Z","iopub.status.idle":"2022-01-06T22:32:34.800296Z","shell.execute_reply.started":"2022-01-06T22:32:34.368280Z","shell.execute_reply":"2022-01-06T22:32:34.799362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"This graph shows the data of host team.","metadata":{}},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"fig = plt.figure(figsize=(12,6))\nvisitor = df1['visitorTeamAbbr'].value_counts()\nsns.barplot(x=visitor.index, y=visitor.values, ci=None)\nplt.xlabel(\"Visitor Team\")\nplt.ylabel(\"Count\")\nplt.xticks(rotation=90);","metadata":{"execution":{"iopub.status.busy":"2022-01-06T22:32:34.802070Z","iopub.execute_input":"2022-01-06T22:32:34.802444Z","iopub.status.idle":"2022-01-06T22:32:35.241206Z","shell.execute_reply.started":"2022-01-06T22:32:34.802395Z","shell.execute_reply":"2022-01-06T22:32:35.240292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# WORKING WITH PLAYERS","metadata":{}},{"cell_type":"code","source":"df2=pd.read_csv('../input/nfl-big-data-bowl-2022/players.csv')\ndf2","metadata":{"execution":{"iopub.status.busy":"2022-01-06T22:32:35.242657Z","iopub.execute_input":"2022-01-06T22:32:35.243550Z","iopub.status.idle":"2022-01-06T22:32:35.277996Z","shell.execute_reply.started":"2022-01-06T22:32:35.243490Z","shell.execute_reply":"2022-01-06T22:32:35.277096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2.info()","metadata":{"execution":{"iopub.status.busy":"2022-01-06T22:32:35.279219Z","iopub.execute_input":"2022-01-06T22:32:35.279547Z","iopub.status.idle":"2022-01-06T22:32:35.295717Z","shell.execute_reply.started":"2022-01-06T22:32:35.279515Z","shell.execute_reply":"2022-01-06T22:32:35.294974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-01-06T22:32:35.296821Z","iopub.execute_input":"2022-01-06T22:32:35.297546Z","iopub.status.idle":"2022-01-06T22:32:35.308049Z","shell.execute_reply.started":"2022-01-06T22:32:35.297495Z","shell.execute_reply":"2022-01-06T22:32:35.307159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2[\"birthYear\"] = 0\ndf2[\"birthMonth\"] = 0\ndf2.dropna(subset=[\"birthDate\"], inplace=True)\nfor idx, row in df2.iterrows():\n    if len(row['birthDate'].split('/')) == 3:  \n        df2.loc[idx, 'birthYear'] = row['birthDate'].split('/')[2]\n        df2.loc[idx, 'birthMonth'] = row['birthDate'].split('/')[0]\n        \n    elif len(row['birthDate'].split('-')) == 3:\n        df2.loc[idx, 'birthYear'] = row['birthDate'].split('-')[0]\n        df2.loc[idx, 'birthMonth'] = row['birthDate'].split('-')[1]","metadata":{"execution":{"iopub.status.busy":"2022-01-06T22:32:35.309517Z","iopub.execute_input":"2022-01-06T22:32:35.309789Z","iopub.status.idle":"2022-01-06T22:32:37.017370Z","shell.execute_reply.started":"2022-01-06T22:32:35.309758Z","shell.execute_reply":"2022-01-06T22:32:37.016367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2.info()","metadata":{"execution":{"iopub.status.busy":"2022-01-06T22:32:37.018722Z","iopub.execute_input":"2022-01-06T22:32:37.018963Z","iopub.status.idle":"2022-01-06T22:32:37.036317Z","shell.execute_reply.started":"2022-01-06T22:32:37.018935Z","shell.execute_reply":"2022-01-06T22:32:37.035219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pl_height = df2[\"height\"] \npl_height = pl_height.apply(lambda x: x.split(\"-\")) \ndf2[\"height\"] =pl_height.apply(lambda x: int(x[0]) * 12 + int(x[1]) if len(x) == 2 else int(x[0])) * 2.54\n\ndf2[\"weight\"] = round(df2.weight * 0.453592, 2)\n\ndf2","metadata":{"execution":{"iopub.status.busy":"2022-01-06T22:32:37.040726Z","iopub.execute_input":"2022-01-06T22:32:37.041060Z","iopub.status.idle":"2022-01-06T22:32:37.075928Z","shell.execute_reply.started":"2022-01-06T22:32:37.041023Z","shell.execute_reply":"2022-01-06T22:32:37.075021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.cm as cm\nfrom matplotlib.colors import rgb2hex\ncmap = cm.get_cmap('GnBu',12) \ncol_def =[]\nfor i in range(cmap.N):\n    rgb = cmap(i)[:3]\n    col_def.append(rgb2hex(rgb))\n    print(rgb2hex(rgb))","metadata":{"execution":{"iopub.status.busy":"2022-01-06T22:32:37.077324Z","iopub.execute_input":"2022-01-06T22:32:37.077641Z","iopub.status.idle":"2022-01-06T22:32:37.089449Z","shell.execute_reply.started":"2022-01-06T22:32:37.077607Z","shell.execute_reply":"2022-01-06T22:32:37.088562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"positon_pl = df2['Position'].value_counts()\nsns.set_style('darkgrid')\nfig, axes = plt.subplots(1,2,figsize=(12,6))\naxes[0] = sns.barplot(x=positon_pl[:10].values, y=positon_pl[:10].index, edgecolor=\"purple\",palette=col_def, ax=axes[0])\naxes[0].set_title(\"Upper Ten Postions played by player (By Count)\", fontsize=18)\naxes[1].pie(x= positon_pl[:10], labels = positon_pl[:10].index, colors=col_def, autopct='%.0f%%',\n           explode=[0.03 for i in positon_pl[:10].index])\naxes[1].add_artist(plt.Circle((0,0),0.5,fc='red'))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-06T22:32:37.090823Z","iopub.execute_input":"2022-01-06T22:32:37.091225Z","iopub.status.idle":"2022-01-06T22:32:37.543720Z","shell.execute_reply.started":"2022-01-06T22:32:37.091193Z","shell.execute_reply":"2022-01-06T22:32:37.542775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10, 6), dpi=100)\nsns.regplot(x=df2.weight, y=df2.height, line_kws={\"color\": \"black\"})\nplt.title(\"Weight(Kg) vs Height(cm)\");","metadata":{"execution":{"iopub.status.busy":"2022-01-06T22:32:37.545016Z","iopub.execute_input":"2022-01-06T22:32:37.545262Z","iopub.status.idle":"2022-01-06T22:32:38.157306Z","shell.execute_reply.started":"2022-01-06T22:32:37.545232Z","shell.execute_reply":"2022-01-06T22:32:38.156214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize=(20, 15), dpi=80)\n\nax1 = fig.add_subplot(223)\nsns.histplot(df2.weight, ax=ax1)\nax1.set_title(\"Weight Distribution\")\n\nax2 = fig.add_subplot(224)\nsns.histplot(df2.height, ax=ax2, bins=10)\nax2.set_title(\"Height Distribution\");","metadata":{"execution":{"iopub.status.busy":"2022-01-06T22:32:38.158887Z","iopub.execute_input":"2022-01-06T22:32:38.159296Z","iopub.status.idle":"2022-01-06T22:32:39.003042Z","shell.execute_reply.started":"2022-01-06T22:32:38.159246Z","shell.execute_reply":"2022-01-06T22:32:39.001964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize=(20, 15), dpi=80)\n\nbirthyear = df2['birthYear'].value_counts()\nax1 = fig.add_subplot(223)\nsns.barplot(x=birthyear.index, y=birthyear.values, ci=None, ax=ax1)\nax1.tick_params(axis='x', rotation=45)\nax1.set_title(\"Birthyear Distribution\",size=20)\nplt.xlabel(\"Year\", size=15)\n\nbirthmonth = df2['birthMonth'].value_counts()\nax2 = fig.add_subplot(224)\nsns.barplot(x=birthmonth.index, y=birthmonth.values, ci=None, ax=ax2)\nax2.set_title(\"Birthmonth Distribution\",size=20)\nplt.xlabel(\"Month\",size=15);","metadata":{"execution":{"iopub.status.busy":"2022-01-06T22:32:39.004598Z","iopub.execute_input":"2022-01-06T22:32:39.004970Z","iopub.status.idle":"2022-01-06T22:32:39.805283Z","shell.execute_reply.started":"2022-01-06T22:32:39.004922Z","shell.execute_reply":"2022-01-06T22:32:39.804230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# WORKING WITH PLAYS DATAFRAME","metadata":{}},{"cell_type":"code","source":"df3 = pd.read_csv('../input/nfl-big-data-bowl-2022/plays.csv')\ndf3['scoreDiff'] = abs(df3.preSnapHomeScore-df3.preSnapVisitorScore)\ndf3.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-06T22:32:39.806937Z","iopub.execute_input":"2022-01-06T22:32:39.807562Z","iopub.status.idle":"2022-01-06T22:32:40.039332Z","shell.execute_reply.started":"2022-01-06T22:32:39.807502Z","shell.execute_reply":"2022-01-06T22:32:40.038576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ((ax1,ax2),(ax3,ax4),(ax5,ax6),(ax7,ax8), (ax9,ax10)) = plt.subplots(5,2, figsize=(15,20))\ndf3.kickLength.plot.hist(bins=50, title='Kick length', grid=True, ax=ax1)\ndf3.loc[df3.kickReturnYardage.notnull()]['kickReturnYardage'].plot.hist(bins=50, title='Return result (yds)', grid=True, ax=ax2)\ndf3.playResult.plot.hist(bins=50, title='Play result (yds)', grid=True, ax=ax3)\ndf3.yardsToGo.plot.hist(bins=20, title='Yards to go at play start', grid=True, ax=ax4)\ndf3.penaltyYards.plot.hist(title='Penalty yards', grid=True, ax=ax5)\ndf3.penaltyCodes.value_counts()[:10].plot.bar(title='Penalty codes (top 10)', ax=ax6)\ndf3.specialTeamsPlayType.value_counts().plot.bar(title='Play type', ax=ax7)\ndf3.specialTeamsResult.value_counts().plot.bar(title='Play result breakdown', ax=ax8)\ndf3.loc[df3.passResult.notnull()]['passResult'].value_counts().plot.bar(title='Pass result breakdown', ax=ax9)\ndf3.yardlineNumber.plot.hist(bins=20, title='Where plays happen (yardline #)', grid=True, ax=ax10)\n\n","metadata":{"execution":{"iopub.status.busy":"2022-01-06T22:32:40.040404Z","iopub.execute_input":"2022-01-06T22:32:40.041183Z","iopub.status.idle":"2022-01-06T22:32:42.834599Z","shell.execute_reply.started":"2022-01-06T22:32:40.041146Z","shell.execute_reply":"2022-01-06T22:32:42.833590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# SCOUTING DATASET","metadata":{}},{"cell_type":"code","source":"df4 = pd.read_csv('../input/nfl-big-data-bowl-2022/PFFScoutingData.csv')\ndf4.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-06T22:32:42.836441Z","iopub.execute_input":"2022-01-06T22:32:42.836998Z","iopub.status.idle":"2022-01-06T22:32:42.955514Z","shell.execute_reply.started":"2022-01-06T22:32:42.836945Z","shell.execute_reply":"2022-01-06T22:32:42.954230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ((ax1,ax2,ax3), (ax4,ax5,ax6)) = plt.subplots(2,3, figsize=(15,8))  \ndf4.hangTime.plot.hist(bins=20, grid=True, title='Hangtime (seconds)', ax=ax1)\ndf4.loc[df4.kickType.notnull()]['kickType'].value_counts().plot.bar(title='Kick type', ax=ax2)\ndf4.loc[df4.kickDirectionActual.notnull()]['kickDirectionActual'].value_counts().plot.bar(title='Kick direction', ax=ax3)\ndf4.loc[df4.snapTime.notnull()]['snapTime'].plot.hist(bins=20, grid=True, title='Snap time', ax=ax4)\ndf4.loc[df4.kickContactType.notnull()]['kickContactType'].value_counts().plot.bar(title='Kick contact type', ax=ax5)\ndf4.loc[df4.returnDirectionActual.notnull()]['returnDirectionActual'].value_counts().plot.bar(title='Return direction', ax=ax6)","metadata":{"execution":{"iopub.status.busy":"2022-01-06T22:32:42.956730Z","iopub.execute_input":"2022-01-06T22:32:42.956980Z","iopub.status.idle":"2022-01-06T22:32:44.581411Z","shell.execute_reply.started":"2022-01-06T22:32:42.956950Z","shell.execute_reply":"2022-01-06T22:32:44.580404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# TRACKING DATASET OF 3 YEARS","metadata":{}},{"cell_type":"code","source":"df5 = pd.read_csv('../input/nfl-big-data-bowl-2022/tracking2020.csv')\ndf6 =  pd.read_csv('../input/nfl-big-data-bowl-2022/tracking2019.csv')\ndf7 =  pd.read_csv('../input/nfl-big-data-bowl-2022/tracking2018.csv')\ndf5.head()\ndf6.head()\ndf7.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-06T22:32:44.582747Z","iopub.execute_input":"2022-01-06T22:32:44.583020Z","iopub.status.idle":"2022-01-06T22:35:15.475287Z","shell.execute_reply.started":"2022-01-06T22:32:44.582987Z","shell.execute_reply":"2022-01-06T22:35:15.474069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Tracking events:')\ndf5.event.unique()","metadata":{"execution":{"iopub.status.busy":"2022-01-06T22:35:15.477062Z","iopub.execute_input":"2022-01-06T22:35:15.477364Z","iopub.status.idle":"2022-01-06T22:35:16.368203Z","shell.execute_reply.started":"2022-01-06T22:35:15.477328Z","shell.execute_reply":"2022-01-06T22:35:16.367252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Tracking events:')\ndf6.event.unique()","metadata":{"execution":{"iopub.status.busy":"2022-01-06T22:35:16.369835Z","iopub.execute_input":"2022-01-06T22:35:16.370127Z","iopub.status.idle":"2022-01-06T22:35:17.285915Z","shell.execute_reply.started":"2022-01-06T22:35:16.370095Z","shell.execute_reply":"2022-01-06T22:35:17.284929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Tracking events:')\ndf7.event.unique()","metadata":{"execution":{"iopub.status.busy":"2022-01-06T22:35:17.287607Z","iopub.execute_input":"2022-01-06T22:35:17.287903Z","iopub.status.idle":"2022-01-06T22:35:18.242152Z","shell.execute_reply.started":"2022-01-06T22:35:17.287848Z","shell.execute_reply":"2022-01-06T22:35:18.241056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df5['ts'] = pd.to_datetime(df5['time']).values.astype(np.int64) // 10 ** 9\ndf5 = df5.drop(columns=['time'])\ndf5.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-06T22:35:18.243853Z","iopub.execute_input":"2022-01-06T22:35:18.244157Z","iopub.status.idle":"2022-01-06T22:35:22.498129Z","shell.execute_reply.started":"2022-01-06T22:35:18.244124Z","shell.execute_reply":"2022-01-06T22:35:22.497170Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df6['ts'] = pd.to_datetime(df6['time']).values.astype(np.int64) // 10 ** 9\ndf6 = df6.drop(columns=['time'])\ndf6.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-06T22:35:22.499395Z","iopub.execute_input":"2022-01-06T22:35:22.499856Z","iopub.status.idle":"2022-01-06T22:35:26.826934Z","shell.execute_reply.started":"2022-01-06T22:35:22.499815Z","shell.execute_reply":"2022-01-06T22:35:26.825948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df7['ts'] = pd.to_datetime(df7['time']).values.astype(np.int64) // 10 ** 9\ndf7 = df7.drop(columns=['time'])\ndf7.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-06T22:35:26.828180Z","iopub.execute_input":"2022-01-06T22:35:26.828409Z","iopub.status.idle":"2022-01-06T22:35:31.241055Z","shell.execute_reply.started":"2022-01-06T22:35:26.828381Z","shell.execute_reply":"2022-01-06T22:35:31.240068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df5.groupby(['playId','nflId']).agg({'x': lambda x: x.iat[-1] - x.iat[0], \n                                       'y': lambda x: x.iat[-1] - x.iat[0], \n                                       's': 'mean',                         \n                                       'dis': 'sum',                       \n                                       'o': 'mean',                         \n                                       'dir': 'mean',                       \n                                       'frameId': 'last',                  \n                                       'ts': lambda x: x.max() - x.min(), \n                                       'position': 'first', \n                                       'team': 'first',\n                                       'playDirection': 'first',\n                                       'event': 'first'})","metadata":{"execution":{"iopub.status.busy":"2022-01-06T22:35:31.242641Z","iopub.execute_input":"2022-01-06T22:35:31.242997Z","iopub.status.idle":"2022-01-06T22:35:48.508367Z","shell.execute_reply.started":"2022-01-06T22:35:31.242951Z","shell.execute_reply":"2022-01-06T22:35:48.507401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df6.groupby(['playId','nflId']).agg({'x': lambda x: x.iat[-1] - x.iat[0], \n                                       'y': lambda x: x.iat[-1] - x.iat[0], \n                                       's': 'mean',                         \n                                       'dis': 'sum',                       \n                                       'o': 'mean',                         \n                                       'dir': 'mean',                       \n                                       'frameId': 'last',                  \n                                       'ts': lambda x: x.max() - x.min(), \n                                       'position': 'first', \n                                       'team': 'first',\n                                       'playDirection': 'first',\n                                       'event': 'first'})","metadata":{"execution":{"iopub.status.busy":"2022-01-06T22:35:48.509731Z","iopub.execute_input":"2022-01-06T22:35:48.509996Z","iopub.status.idle":"2022-01-06T22:36:05.982984Z","shell.execute_reply.started":"2022-01-06T22:35:48.509963Z","shell.execute_reply":"2022-01-06T22:36:05.982210Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df7.groupby(['playId','nflId']).agg({'x': lambda x: x.iat[-1] - x.iat[0], \n                                       'y': lambda x: x.iat[-1] - x.iat[0], \n                                       's': 'mean',                         \n                                       'dis': 'sum',                       \n                                       'o': 'mean',                         \n                                       'dir': 'mean',                       \n                                       'frameId': 'last',                  \n                                       'ts': lambda x: x.max() - x.min(), \n                                       'position': 'first', \n                                       'team': 'first',\n                                       'playDirection': 'first',\n                                       'event': 'first'})","metadata":{"execution":{"iopub.status.busy":"2022-01-06T22:36:05.984283Z","iopub.execute_input":"2022-01-06T22:36:05.984783Z","iopub.status.idle":"2022-01-06T22:36:24.498894Z","shell.execute_reply.started":"2022-01-06T22:36:05.984737Z","shell.execute_reply":"2022-01-06T22:36:24.497889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ball_df = pd.merge(df5.loc[df5.team=='football'], df3, how='left', on=['gameId','playId'])\n\nfig, ((ax1,ax2),(ax3,ax4)) = plt.subplots(2,2, figsize=(15,10))  \nball_df.loc[ball_df.specialTeamsPlayType=='Kickoff'].\\\n    groupby('frameId')['s'].mean()[:100]\\\n    .plot.line(figsize=(10,6),\n               title='Avg. ball speed on kickoff plays (first 100 frames)', \n               ylabel='speed',\n               ax=ax1)\nball_df.loc[ball_df.specialTeamsPlayType=='Punt'].\\\n    groupby('frameId')['s'].mean()[:100]\\\n    .plot.line(figsize=(10,6),\n               title='Avg. ball speed on punt plays (first 100 frames)', \n               ylabel='speed',\n               ax=ax2)\nball_df.loc[ball_df.specialTeamsPlayType=='Field Goal'].\\\n    groupby('frameId')['s'].mean()[:100]\\\n    .plot.line(figsize=(10,6),\n               title='Avg. ball speed on field goal plays (first 100 frames)', \n               ylabel='speed',\n               ax=ax3)\nball_df.loc[ball_df.specialTeamsPlayType=='Extra Point'].\\\n    groupby('frameId')['s'].mean()[:100]\\\n    .plot.line(figsize=(10,6),\n               title='Avg. ball speed on extra point plays (first 100 frames)', \n               ylabel='speed',\n               ax=ax4)\nplt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2022-01-06T22:36:24.500214Z","iopub.execute_input":"2022-01-06T22:36:24.500450Z","iopub.status.idle":"2022-01-06T22:36:28.646497Z","shell.execute_reply.started":"2022-01-06T22:36:24.500422Z","shell.execute_reply":"2022-01-06T22:36:28.645809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ball_df = pd.merge(df6.loc[df6.team=='football'], df3, how='left', on=['gameId','playId'])\n\nfig, ((ax1,ax2),(ax3,ax4)) = plt.subplots(2,2, figsize=(15,10))  \nball_df.loc[ball_df.specialTeamsPlayType=='Kickoff'].\\\n    groupby('frameId')['s'].mean()[:100]\\\n    .plot.line(figsize=(10,6),\n               title='Avg. ball speed on kickoff plays (first 100 frames)', \n               ylabel='speed',\n               ax=ax1)\nball_df.loc[ball_df.specialTeamsPlayType=='Punt'].\\\n    groupby('frameId')['s'].mean()[:100]\\\n    .plot.line(figsize=(10,6),\n               title='Avg. ball speed on punt plays (first 100 frames)', \n               ylabel='speed',\n               ax=ax2)\nball_df.loc[ball_df.specialTeamsPlayType=='Field Goal'].\\\n    groupby('frameId')['s'].mean()[:100]\\\n    .plot.line(figsize=(10,6),\n               title='Avg. ball speed on field goal plays (first 100 frames)', \n               ylabel='speed',\n               ax=ax3)\nball_df.loc[ball_df.specialTeamsPlayType=='Extra Point'].\\\n    groupby('frameId')['s'].mean()[:100]\\\n    .plot.line(figsize=(10,6),\n               title='Avg. ball speed on extra point plays (first 100 frames)', \n               ylabel='speed',\n               ax=ax4)\nplt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2022-01-06T22:36:28.647819Z","iopub.execute_input":"2022-01-06T22:36:28.648220Z","iopub.status.idle":"2022-01-06T22:36:32.898532Z","shell.execute_reply.started":"2022-01-06T22:36:28.648186Z","shell.execute_reply":"2022-01-06T22:36:32.895384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ball_df = pd.merge(df7.loc[df7.team=='football'], df3, how='left', on=['gameId','playId'])\n\nfig, ((ax1,ax2),(ax3,ax4)) = plt.subplots(2,2, figsize=(15,10))  \nball_df.loc[ball_df.specialTeamsPlayType=='Kickoff'].\\\n    groupby('frameId')['s'].mean()[:100]\\\n    .plot.line(figsize=(10,6),\n               title='Avg. ball speed on kickoff plays (first 100 frames)', \n               ylabel='speed',\n               ax=ax1)\nball_df.loc[ball_df.specialTeamsPlayType=='Punt'].\\\n    groupby('frameId')['s'].mean()[:100]\\\n    .plot.line(figsize=(10,6),\n               title='Avg. ball speed on punt plays (first 100 frames)', \n               ylabel='speed',\n               ax=ax2)\nball_df.loc[ball_df.specialTeamsPlayType=='Field Goal'].\\\n    groupby('frameId')['s'].mean()[:100]\\\n    .plot.line(figsize=(10,6),\n               title='Avg. ball speed on field goal plays (first 100 frames)', \n               ylabel='speed',\n               ax=ax3)\nball_df.loc[ball_df.specialTeamsPlayType=='Extra Point'].\\\n    groupby('frameId')['s'].mean()[:100]\\\n    .plot.line(figsize=(10,6),\n               title='Avg. ball speed on extra point plays (first 100 frames)', \n               ylabel='speed',\n               ax=ax4)\nplt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2022-01-06T22:36:32.899796Z","iopub.execute_input":"2022-01-06T22:36:32.900049Z","iopub.status.idle":"2022-01-06T22:36:37.289629Z","shell.execute_reply.started":"2022-01-06T22:36:32.900020Z","shell.execute_reply":"2022-01-06T22:36:37.288922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Finding Fake Player","metadata":{}},{"cell_type":"code","source":"all_fakes = {'2018':'','2019':'','2020':''}\nfor year in all_fakes:\n    print(f'Loading {year} data....')\n    df = pd.read_csv(f'../input/nfl-big-data-bowl-2022/tracking{year}.csv')\n    print(f'Filtering fake play data....')\n    fake_play_list = df.loc[df.event.str.contains('fake')]['playId'].unique().tolist()\n    all_fakes[year] = df.loc[df.playId.isin(fake_play_list)]\n    print(f'Freeing memory....')\n    del df\n    gc.collect()\n    print('Completed.')\n    \nfake_df = all_fakes['2018'].append(all_fakes['2019']).append(all_fakes['2020'])\n\nprint(f'\\nShape of fake dataframe: {fake_df.shape}\\n')\nfake_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-06T22:36:37.290584Z","iopub.execute_input":"2022-01-06T22:36:37.290849Z","iopub.status.idle":"2022-01-06T22:38:42.116796Z","shell.execute_reply.started":"2022-01-06T22:36:37.290819Z","shell.execute_reply":"2022-01-06T22:38:42.115633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Drawing the Plot for player position","metadata":{}},{"cell_type":"code","source":"plt.rcParams['figure.figsize'] = [20, 16]\ndef drawPitch(width, height, color=\"w\"):\n    fig = plt.figure()\n    ax = plt.axes(xlim=(-10, width + 30), ylim=(-15, height + 5))\n    plt.axis('off')\n\n    # Grass around pitch\n    rect = patches.Rectangle((-10, -5), width + 40, height + 10, linewidth=1, facecolor='#3f995b', capstyle='round')\n    ax.add_patch(rect)\n\n\n    # Pitch boundaries\n    rect = plt.Rectangle((0, 0), width + 20, height, ec=color, fc=\"None\", lw=2)\n    ax.add_patch(rect)\n   \n\n    # vertical lines - every 5 yards\n    for i in range(21):\n        plt.plot([10 + 5 * i, 10 + 5 * i], [0, height], c=\"w\", lw=2)\n    \n        \n    # distance markers - every 10 yards\n    for yards in range(10, width, 10):\n        yards_text = yards if yards <= width / 2 else width - yards\n        # top markers\n        plt.text(10 + yards - 2, height - 7.5, yards_text, size=20, c=\"w\", weight=\"bold\")\n        # botoom markers\n        plt.text(10 + yards - 2, 7.5, yards_text, size=20, c=\"w\", weight=\"bold\", rotation=180)\n   \n\n    # yards markers - every yard\n    # bottom markers\n    for x in range(20):\n        for j in range(1, 5):\n            plt.plot([10 + x * 5 + j, 10 + x * 5 + j], [1, 3], color=\"w\", lw=3)\n\n    # top markers\n    for x in range(20):\n        for j in range(1, 5):\n            plt.plot([10 + x * 5 + j, 10 + x * 5 + j], [height - 1, height - 3], color=\"w\", lw=3)\n\n    # middle bottom markers\n    y = (height - 18.5) / 2\n    for x in range(20):\n        for j in range(1, 5):\n            plt.plot([10 + x * 5 + j, 10 + x * 5 + j], [y, y + 2], color=\"w\", lw=3)\n\n    # middle top markers\n    for x in range(20):\n        for j in range(1, 5):\n            plt.plot([10 + x * 5 + j, 10 + x * 5 + j], [height - y, height - y - 2], color=\"w\", lw=3)\n   \n\n    # draw home end zone\n    plt.text(2.5, (height - 10) / 2, \"HOME\", size=40, c=\"w\", weight=\"bold\", rotation=90)\n    rect = plt.Rectangle((0, 0), 10, height, ec=color, fc=\"#0064dc\", lw=2)\n    ax.add_patch(rect)\n\n    # draw away end zone    \n    plt.text(112.5, (height - 10) / 2, \"AWAY\", size=40, c=\"w\", weight=\"bold\", rotation=-90)\n    rect = plt.Rectangle((width + 10, 0), 10, height, ec=color, fc=\"#c80014\", lw=2)\n    ax.add_patch(rect)\n    \n    # draw extra spot point\n    # left\n    y = (height - 3) / 2\n    plt.plot([10 + 2, 10 + 2], [y, y + 3], c=\"w\", lw=2)\n    \n    # right\n    plt.plot([width + 10 - 2, width + 10 - 2], [y, y + 3], c=\"w\", lw=2)\n    \n    # draw goalpost\n    goal_width = 6 # yards\n    y = (height - goal_width) / 2\n    # left\n    plt.plot([0, 0], [y, y + goal_width], \"-\", c=\"y\", lw=10, ms=20)\n    # right\n    plt.plot([width + 20, width + 20], [y, y + goal_width], \"-\", c=\"y\", lw=10, ms=20)\n    \n    return fig, ax\n\nfig, ax = drawPitch(100, 53.3)","metadata":{"execution":{"iopub.status.busy":"2022-01-06T22:38:42.118884Z","iopub.execute_input":"2022-01-06T22:38:42.119710Z","iopub.status.idle":"2022-01-06T22:38:42.974109Z","shell.execute_reply.started":"2022-01-06T22:38:42.119649Z","shell.execute_reply":"2022-01-06T22:38:42.973282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Conclusion -\nFinally it has completed and I also took help from different source . It was a great project!","metadata":{}}]}