我正在尝试将来自patentview API的JSON转换为pandas数据帧。但是它很难,因为它似乎是一个嵌套的JSON!
这是我的代码:
import requests
import pandas as pd
from pandas.io.json import json_normalize
import json
url = 'http://www.patentsview.org/api/patents/query?q={"cpc_group_id":"B60W"}&f=["inventor_first_name","inventor_last_name","patent_number", "assignee_country"]'
r = requests.get(url)
json_data = r.json()
df = pd.DataFrame(json_data['patents'])
df
查看返回的数据帧的图像。
我的问题是如何将嵌套字典键及其值放入唯一列?
答案 0 :(得分:1)
result = pd.DataFrame.from_records(df.assignees.apply(lambda x: x[0]))
inventors = pd.DataFrame.from_records(df.inventors.apply(lambda x: x[0]))
result = pd.concat([result, inventors, df.patent_number], axis=1)
result.head()
assignee_country assignee_key_id inventor_first_name inventor_key_id \
0 None None Robert Cecil 4848
1 GB 82078 Anthony John 16057
2 None None James W. 16376
3 FR 281289 Gilles 18482
4 JP 301319 Kiyoharu 18507
inventor_last_name patent_number
0 Clerk 3932991
1 Adey 3939738
2 Moberg 3939937
3 Leconte 3941203
4 Murakami 3941223