{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "**Table of Contents**\n", "\n", "
\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Super fun demo from moderndata!\n", "http://moderndata.plot.ly/nba-shots-analysis-using-plotly-shapes/" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import requests as r\n", " \n", "# Chrome's user-agent string, to simulate a browser visiting the webpage\n", "headers = {\n", " 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36'\n", "}\n", " \n", "# Stephen Curry's player id\n", "player_id = 201939\n", " \n", "# season details\n", "season = '2015-16'\n", "season_type = 'Regular Season'\n", " \n", "# request parameters\n", "req_params = {\n", " 'AheadBehind': '',\n", " 'ClutchTime': '',\n", " 'ContextFilter': '',\n", " 'ContextMeasure': 'FGA',\n", " 'DateFrom': '',\n", " 'DateTo': '',\n", " 'EndPeriod': '',\n", " 'EndRange': '',\n", " 'GameID': '',\n", " 'GameSegment': '',\n", " 'LastNGames': 0,\n", " 'LeagueID': '00',\n", " 'Location': '',\n", " 'Month': 0,\n", " 'OpponentTeamID': 0,\n", " 'Outcome': '',\n", " 'Period': 0,\n", " 'PlayerID': player_id,\n", " 'PointDiff': '',\n", " 'Position': '',\n", " 'RangeType': '',\n", " 'RookieYear': '',\n", " 'Season': season,\n", " 'SeasonSegment': '',\n", " 'SeasonType': season_type,\n", " 'StartPeriod': '',\n", " 'StartRange': '',\n", " 'TeamID': 0,\n", " 'VsConference': '',\n", " 'VsDivision': ''\n", "}\n", " \n", "res = r.get('http://stats.nba.com/stats/shotchartdetail', params=req_params, headers=headers)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Data Transformation for querying" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import pandas as pd\n", " \n", "res_json = res.json()\n", " \n", "# column names\n", "rows = res_json['resultSets'][0]['headers']\n", "# row content\n", "shots_data = res_json['resultSets'][0]['rowSet']\n", " \n", "shots_df = pd.DataFrame(shots_data, columns=rows)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([u'Made Shot', u'Missed Shot'], dtype=object)" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "shots_df['EVENT_TYPE'].unique()\n", "\n", "np.array([u'Made Shot', u'Missed Shot'], dtype=object)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Shot locations" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "