[英]Dictionary with Nested Lists to Dataframe
我目前有一本帶有一些嵌套列表的字典,這有點混亂:
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]]}
並使用熊貓,我希望將其放入一個表格,其中我的標題是headers
列表中的所有值,並且每一行均以rowSet
的值命名,如此處所示的表: http : //stats.nba。 COM /播放/ 2544 /鏡頭儀表板/?排序= G&DIR = -1
我已經嘗試了df = pd.DataFrame(list(table.items()))
東西,但是它們都返回了非常混亂的表,所以我不確定如何處理嵌套的東西這本字典。
其余代碼為:
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')
有什么辦法可以在幾行中做到這一點,還是我需要走一條更手動的路線?
df=pd.DataFrame(data=table['rowSet'], columns=table['headers'])
輸出:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.