【问题标题】:Can only extract some of the data from a JSON file只能从 JSON 文件中提取部分数据
【发布时间】:2018-07-19 13:27:01
【问题描述】:

我正在从 API ('http://api.eia.gov/') 导入 JSON 文件。以下三行非常适合将时间序列数据放入数据框中:

d = json.loads(requests.get(page).text)
df = pd.DataFrame(d['series'][0]['data'])
df = df.set_index(df[0])

我的问题是提取与出现在字典末尾的数据无关的其他值/元素。这是我在终端中输入 d 时的一些输出。

In [72]: d
Out[72]: 
{'request': {'command': 'series', 'series_id': 'STEO.COPR_AG.M'},
 'series': [{'series_id': 'STEO.COPR_AG.M',
  'name': 'Crude Oil Production, Algeria, Monthly',
  'units': 'million barrels per day',
  'f': 'M',
  'copyright': 'None',
  'source': 'U.S. Energy Information Administration (EIA) - Short Term Energy 
             Outlook',
  'geography': 'DZA',
  'start': '199401',
  'end': '201806',
  'lastHistoricalPeriod': '201803',
  'updated': '2018-07-10T12:02:18-0400',
  'data': [['201806', 1.04],
   ['201805', 1.02],
   ['201804', 0.99],
   ['201803', 0.99], 

因此,使用上面的 df 可以为我提供时间序列的数据帧数据。但我不知道如何获取其他一些变量——比如“名称”或“lastHistoricalPeriod”

提前致谢。

【问题讨论】:

    标签: json python-3.x dataframe


    【解决方案1】:

    我想通了。
    我还需要包括系列...不确定我是否理解为什么的逻辑,但这有效。

    In [90]: x = d['series'][0]['name']
    In [91]: print(x)
    Crude Oil Production, Algeria, Monthly
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-28
      • 2023-02-07
      • 2017-04-03
      • 2019-01-22
      • 2015-08-27
      • 2018-05-20
      • 1970-01-01
      相关资源
      最近更新 更多