【问题标题】:Extract data from API and store the records in a Pandas dataframe从 API 中提取数据并将记录存储在 Pandas 数据框中
【发布时间】:2022-01-19 16:19:36
【问题描述】:

我正在查看以下网站:https://data.gov.sg/dataset/bunker-sales-monthly?resource_id=44da3191-6c57-4d4a-8268-8e2c418d4b43,他们有以下使用 API 提取数据的示例:

    import requests
 
    result = []
    headers = {'User-Agent': 'Mozilla/5.0'}
    url = "https://data.gov.sg/api/action/datastore_search?resource_id=44da3191-6c57-4d4a-8268-8e2c418d4b43"
    r = requests.get(url, headers=headers)
    data = r.json()
    print(data)

这会产生以下内容,我只想从输出中提取“记录”位,并将其转换为更易读的格式。理想情况下,我希望这些数据位于 pandas 数据框中:

{'records': [{'bunker_type': 'Marine Gas Oil', 'bunker_sales': '135.4', '_id': 1, 'month': '1995-01'}, {'bunker_type': '船用柴油机 石油','bunker_sales':'67.9','_id':2,'月':'1995-01'}, {'bunker_type': '船用燃油 180 cst', 'bunker_sales': '412.9', '_id': 3, 'month': '1995-01'}, {'bunker_type': '船用燃油 380 cst','bunker_sales':'820.3','_id':4,'month':'1995-01'}, {'bunker_type': '船用燃油 500 cst +', 'bunker_sales': '0', '_id': 5, '月': '1995-01'}...

格式如下:

df= pd.DataFrame(columns=['month', 'bunker_type', 'bunker_sales'])

我该如何提取这些数据?

【问题讨论】:

    标签: python python-3.x pandas dataframe


    【解决方案1】:

    试试pd.json_normalize:

    df = pd.json_normalize(data['result'], 'records')
    print(df)
    
    # Output:
                      bunker_type bunker_sales  _id    month
    0              Marine Gas Oil        135.4    1  1995-01
    1           Marine Diesel Oil         67.9    2  1995-01
    2     Marine Fuel Oil 180 cst        412.9    3  1995-01
    3     Marine Fuel Oil 380 cst        820.3    4  1995-01
    4   Marine Fuel Oil 500 cst +            0    5  1995-01
    ..                        ...          ...  ...      ...
    95  Ultra Low-Sulfur Fuel Oil            0   95  1995-08
    96                     Others        102.2   96  1995-08
    97             Marine Gas Oil        101.7   97  1995-09
    98          Marine Diesel Oil           63   98  1995-09
    99    Marine Fuel Oil 180 cst          395   99  1995-09
    
    [100 rows x 4 columns]
    

    【讨论】:

      猜你喜欢
      • 2020-12-16
      • 1970-01-01
      • 1970-01-01
      • 2017-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多