【问题标题】:How do I convert data in dictionary format from a url into a pandas dataframe?如何将字典格式的数据从 url 转换为 pandas 数据框?
【发布时间】:2021-07-11 07:06:50
【问题描述】:

【问题讨论】:

    标签: json pandas dataframe dictionary


    【解决方案1】:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-03
      • 2019-02-22
      • 2017-07-16
      • 2020-12-22
      • 1970-01-01
      • 2018-09-12
      • 1970-01-01
      相关资源
      最近更新 更多