我正在尝试规范化Json
{
...
"scoring":[
{
"id": "9e97e6fa-8fdb-4452-b930-d5c0b1ae4a6a",
"full_name": "Tom Washington",
"first_name": "Tom",
"last_name": "Washington",
"number": "49",
"assignment": "Head Official",
"experience": "27"
},
{
"id": "c3f40018-e566-49d5-ad2a-6b7a2af0dfcc",
"full_name": "J.T. Orr",
"first_name": "J.T.",
"last_name": "Orr",
"number": "72"
},
{
"id": "4362601e-767f-4b28-8b1a-9eb6606f2e36",
"full_name": "Rodney Mott",
"first_name": "Rodney",
"last_name": "Mott",
"number": "71",
"assignment": "Official",
"experience": "21"
}
]
...
}
我正在从URL加载json
import json
import pandas as pd
from pandas.io.json import json_normalize #package for flattening json in pandas df
import urllib.request,json
from urllib.request import urlopen
import time
def fetchMatchDetails(url,csvPath):
#url='https://api.sportradar.us/nba/xxxx'
with urllib.request.urlopen(url)as url:
d = json.loads(url.read().decode())
gamesDf = json_normalize(d['scoring'])
newDf=gamesDf[['id','full_name','first_name','last_name','number','assignment','experience']]
但有时在last_name或数字之间缺少少数列,在这种情况下,它在Python中显示为 key_error
如何为不存在的键设置一个nan或null值。
答案 0 :(得分:0)
如果我理解您的要求正确,那么您的代码已经可以正常工作了。我试过了,它为缺少的键返回了NaN。到底是什么问题?
full_name first_name last_name number assignment experience
0 Tom Washington Tom Washington 49 Head Official 27
1 J.T. Orr J.T. Orr 72 NaN NaN
2 Rodney Mott Rodney Mott 71 Official 21