{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"markdown","source":"# Get the Speed of the Players Involved in Injury\n#### Given a punt play with an injury, the functions below will return a data frame with the max speed (in miles/hour) and avg speed for both the injured player and the primary partner."},{"metadata":{"trusted":true,"_uuid":"2ef2dd155a9245d7bba59416a4a0f88cc7fd04c6"},"cell_type":"code","source":"#import packages\nimport numpy as np\nimport pandas as pd","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"0fb5aa10f4f824e0715590e98b20cd785a48ae08"},"cell_type":"markdown","source":" The Net Gen Stats data frames have a distance column called 'dis'.  Each value in this column shows the yard per tenth of a second traveled.  We can convert this column to miles per hour for easier use."},{"metadata":{"trusted":true,"_uuid":"22c9bffdd82bb189e0b21595fcfd947c9a81b9f7"},"cell_type":"code","source":"def convert_to_mph(dis_vector, converter):\n    mph_vector = dis_vector * converter\n    return mph_vector","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b82af3a10ab1e3a1ff0d37f0c69a82f1ec4f3161"},"cell_type":"markdown","source":"The conversion from 1 yard/second to miles/hour is 1 * 2.04545.  Since each row is a tenth of a second, we need to  also multiply by 10.\n\nThe main fuction below reads in one of the csv's and filters for one of the specific punt plays were an injury occured.  It then converts the distance field to MPH and returns the max and mean speeds for the player injured [1st row] and primary partner [2nd row]"},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"def get_speed(ng_data, playId, gameKey, player, partner):\n    ng_data = pd.read_csv(ng_data)\n    ng_data['mph'] = convert_to_mph(ng_data['dis'], 20.455)\n    player_data = ng_data.loc[(ng_data.GameKey == gameKey) & (ng_data.PlayID == playId) \n                               & (ng_data.GSISID == player)].sort_values('Time')\n    partner_data = ng_data.loc[(ng_data.GameKey == gameKey) & (ng_data.PlayID == playId) \n                              & (ng_data.GSISID == partner)].sort_values('Time')\n    player_grouped = player_data.groupby(['GameKey','PlayID','GSISID'], \n                               as_index = False)['mph'].agg({'max_mph': max,\n                                                             'avg_mph': np.mean\n                                                            })\n    player_grouped['involvement'] = 'player_injured'\n    partner_grouped = partner_data.groupby(['GameKey','PlayID','GSISID'], \n                               as_index = False)['mph'].agg({'max_mph': max,\n                                                             'avg_mph': np.mean\n                                                            })\n    partner_grouped['involvement'] = 'primary_partner'\n    return pd.concat([player_grouped, partner_grouped], axis = 0)[['involvement',\n                                                                   'max_mph',\n                                                                   'avg_mph']].reset_index(drop=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4ad02d73fe5ac5d0348fab6e5e5793d743d21a59"},"cell_type":"code","source":"#Run an example\nget_speed('../input/NGS-2016-pre.csv', 3129, 5, 31057, 32482)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"0657a127690c194fe556bc8009675a3b063713dd"},"cell_type":"markdown","source":"This is just a small snippet of code that I've been using in my project (which is still very much a work in progress).  I hope to continue to contribute little nuggets like this as I work towards a conclusion."},{"metadata":{"trusted":true,"_uuid":"021e4efc99b9fbf2a37bab0b88353f376df9adc1"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}