Json 如何将字典格式的数据从url转换为数据帧?
这是包含我希望转换为数据帧的数据的地址:Json 如何将字典格式的数据从url转换为数据帧?,json,pandas,dataframe,dictionary,Json,Pandas,Dataframe,Dictionary,这是包含我希望转换为数据帧的数据的地址: 我希望标题是大陆ID、大陆、国家ID、国家等等。我该怎么做呢?请求和json\u规范化就是这样做的: 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+
我希望标题是大陆ID、大陆、国家ID、国家等等。我该怎么做呢?请求和json\u规范化就是这样做的:
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