gpt4 book ai didi

json - 如何将字典格式的数据从 url 转换为 Pandas 数据帧?

转载 作者:行者123 更新时间:2023-12-04 07:47:56 25 4
gpt4 key购买 nike

最佳答案

requests 和 pandas json_normalize 可以解决问题:

import pandas as pd
import requests

URL='https://oec.world/olap-proxy/data?cube=trade_i_baci_a_92&Exporter+Country=nausa&HS4=42204&Year=2016&drilldowns=Importer+Country&locale=en&measures=Trade+Value&parents=true&sparse=false&properties=Importer+Country+ISO+3&q=Trade%20Value'

content = requests.get(URL)

d = content.json() # parses json
print(d.keys()) # shows the keys containing the data

df=pd.json_normalize(d['data']) # converts json data into dataframe
print(df.head())
输出:
dict_keys(['data', 'source'])
Continent ID Continent Country ID Country ISO 3 Trade Value
0 af Africa afago Angola ago 57107.0
1 af Africa afben Benin ben 928821.0
2 af Africa afbfa Burkina Faso bfa 6998.0
3 af Africa afciv Cote d'Ivoire civ 55066.0
4 af Africa afcmr Cameroon cmr 53461.0

关于json - 如何将字典格式的数据从 url 转换为 Pandas 数据帧?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67122118/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com