I currently have a dictionary with some nested lists that's a bit messy:
table = {"name":"ClosestDefender10ftPlusShooting", "headers":["PLAYER_ID","PLAYER_NAME_LAST_FIRST","SORT_ORDER","GP","G", "CLOSE_DEF_DIST_RANGE","FGA_FREQUENCY","FGM","FGA","FG_PCT","EFG_PCT","FG2A_FREQUENCY","FG2M","FG2A","FG2_PCT","FG3A_FREQUENCY","FG3M","FG3A","FG3_PCT"], "rowSet":[[2544,"James, LeBron",1,6,2,"0-2 Feet - Very Tight",0.014,0,0.33,0,0,0.007,0,0.17,0,0.007,0,0.17,0],[2544,"James, LeBron",2,6,6,"2-4 Feet - Tight",0.144,0.83,3.5,0.238,0.31,0.082,0.33,2,0.167,0.062,0.5,1.5,0.333],[2544,"James, LeBron",3,6,5,"4-6 Feet - Open",0.192,2.17,4.67,0.464,0.571,0.103,1.17,2.5,0.467,0.089,1,2.17,0.462],[2544,"James, LeBron",4,6,6,"6+ Feet - Wide Open",0.11,1.33,2.67,0.5,0.656,0.041,0.5,1,0.5,0.068,0.83,1.67,0.5]]}
and using pandas I'd like to get it into a table where my headers are all values in the headers
list, and the each row is named after the values in rowSet
, like the tables shown here: http://stats.nba.com/player/2544/shots-dash/?sort=G&dir=-1
I've tried a few things like df = pd.DataFrame(list(table.items()))
and things of that nature but they all return pretty scrambled tables, and so I'm not sure how to deal with the nested nature of this dictionary.
The rest of the code is:
import requests
import pandas as pd
url = 'http://stats.nba.com/stats/playerdashptshots?DateFrom=&DateTo=&GameSegment=&LastNGames=6&LeagueID=00&Location=&Month=0&OpponentTeamID=0&Outcome=&PerMode=PerGame&Period=0&PlayerID=2544&Season=2017-18&SeasonSegment=&SeasonType=Playoffs&TeamID=0&VsConference=&VsDivision='
response = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'})
table = response.json()['resultSets'][5]
data_headers = table.get('headers')
is there any way to do this in a few lines, or do I need to go down a more manual route?
df=pd.DataFrame(data=table['rowSet'], columns=table['headers'])
output:
PLAYER_ID PLAYER_NAME_LAST_FIRST SORT_ORDER GP G CLOSE_DEF_DIST_RANGE \
0 2544 James, LeBron 1 6 2 0-2 Feet - Very Tight
1 2544 James, LeBron 2 6 6 2-4 Feet - Tight
2 2544 James, LeBron 3 6 5 4-6 Feet - Open
3 2544 James, LeBron 4 6 6 6+ Feet - Wide Open
FGA_FREQUENCY FGM FGA FG_PCT EFG_PCT FG2A_FREQUENCY FG2M FG2A \
0 0.014 0.00 0.33 0.000 0.000 0.007 0.00 0.17
1 0.144 0.83 3.50 0.238 0.310 0.082 0.33 2.00
2 0.192 2.17 4.67 0.464 0.571 0.103 1.17 2.50
3 0.110 1.33 2.67 0.500 0.656 0.041 0.50 1.00
FG2_PCT FG3A_FREQUENCY FG3M FG3A FG3_PCT
0 0.000 0.007 0.00 0.17 0.000
1 0.167 0.062 0.50 1.50 0.333
2 0.467 0.089 1.00 2.17 0.462
3 0.500 0.068 0.83 1.67 0.500
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.