解析这种数据可以直接用:json_normalize()
数据:
data=[{'state':'Florida',... 'shortname':'FL',... 'info':{'governor':'Rick Scott'},... 'counties':[{'name':'Dade','population':12345},... {'name':'Broward','population':40000},... {'name':'Palm Beach','population':60000}]},... {'state':'Ohio',... 'shortname':'OH',... 'info':{'governor':'John Kasich'},... 'counties':[{'name':'Summit','population':1234},... {'name':'Cuyahoga','population':1337}]}]
所用命令:
result=json_normalize(data,'counties',['state','shortname',['info','governor']])
结果:
是数据框,如图所示:
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.json_normalize.html
这个里面 还是有个bug的 ,我自己的数据也是 这个样子,但是就是解析的时候一直报错。
这是版本的问题,详见:
https://github.com/pandas-dev/pandas/issues/31507
网友评论