我希望标题是大陆 ID、大陆、国家/地区 ID、国家/地区等。我该怎么做?
答案 0 :(得分:2)
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