简体   繁体   English

将 JSON API 放入 Pandas Dataframe

[英]Putting JSON API in Pandas Dataframe

I have a problem converting a JSON API into a pandas dataframe.我在将 JSON API 转换为 Pandas 数据帧时遇到问题。 I have the following structure of the json file:我有以下 json 文件结构:

{"place":{"AMS":[{"UTC":"14-11-2017 10:00","ValidUTC":"14-11-2017 00:00","Cardinality":"4",...},{"UTC":"14-11-2017 11:00",...}]}} {"place":{"AMS":[{"UTC":"14-11-2017 10:00","ValidUTC":"14-11-2017 00:00","Cardinality":"4", ...},{"UTC":"14-11-2017 11:00",...}]}}

Now in my pandas DataFrame I want the columns, UTC, ValidUtc, cardinality, etc. So I tried to use the Json normalize function:现在在我的 Pandas DataFrame 中,我想要列、UTC、ValidUtc、基数等。所以我尝试使用 Json normalize 函数:

main_api = ('https://api.xxx")
url=main_api
json_data = requests.get(url).json()
df = json_normalize(json_data, 'place', ['AMS'])

and

main_api = ('https://api.xxx")
url=main_api
json_data = requests.get(url).json()
df = json_normalize(json_data, 'place')
df = json_normalize(json_data, 'AMS')

but they do not seem to work.但它们似乎不起作用。 Anyone has an idea about how to convert the json correctly in the pandas DataFrame.任何人都知道如何在 Pandas DataFrame 中正确转换 json。

Refer to JSON to pandas DataFrame it is very well described for normalizing JSON.参考JSON 到 Pandas DataFrame,它很好地描述了规范化 JSON。 Besides, you can to pass a parsing function for the json columns.此外,您可以为 json 列传递解析函数。

Not sure if I recreated your input correctly (you should add the full json without ...).不确定我是否正确地重新创建了您的输入(您应该添加完整的 json 而不...)。

data = {"place":{"AMS":[{"UTC":"14-11-2017 10:00","ValidUTC":"14-11-2017 00:00","Cardinality":"4"}, {"UTC":"15-11-2017 10:00","ValidUTC":"15-11-2017 00:00","Cardinality":"5"} ]}}

pd.json_normalize(data['place']['AMS'])

Output输出

                UTC          ValidUTC Cardinality
0  14-11-2017 10:00  14-11-2017 00:00           4
1  15-11-2017 10:00  15-11-2017 00:00           5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM