【问题标题】:Trying to extract data from JSON URL into Pandas试图从 JSON URL 中提取数据到 Pandas
【发布时间】:2020-08-20 05:16:16
【问题描述】:

我正在尝试将 JSON URL 中的数据提取到 pandas 中,但该文件有多个列表和字典“层”,我似乎无法导航。

import json
from urllib.request import urlopen

with urlopen('https://statdata.pgatour.com/r/010/2020/player_stats.json') as response:
    source = response.read()

data = json.loads(source)

for item in data['tournament']['players']:
    pid = item['pid']
    statId = item['stats']['statId']
    name = item['stats']['name']
    tValue = item['stats']['tValue']
    print(pid, statId, name, tValue)

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-84-eadd8bdb34cb> in <module>
      1 for item in data['tournament']['players']:
      2     player_id = item['pid']
----> 3     stat_id = item['stats']['statId']
      4     stat_name = item['stats']['name']
      5     stat_value = item['stats']['tValue']

TypeError: list indices must be integers or slices, not str

我想要得到的输出是这样的:-

【问题讨论】:

    标签: python arrays json pandas


    【解决方案1】:

    你少了一层。

    为了简化数据,我们尝试访问:

    "stats": [{
        "statId":"106",
        "name":"Eagles",
        "tValue":"0",
    }]
    

    “stats”的数据以[{开头。这是数组中的字典。

    认为这应该可行:

    for item in data['tournament']['players']:
        pid = item['pid']
        for stat in item['stats']:
            statId = stat['statId']
            name = stat['name']
            tValue = stat['tValue']
            print(pid, statId, name, tValue)
    

    阅读更多词典:https://realpython.com/iterate-through-dictionary-python/

    【讨论】:

      【解决方案2】:

      正如前面的答案所暗示的,statsstat 项目的列表。这将向您展示发生了什么,并发现任何其他问题:

      import json
      from urllib.request import urlopen
      
      with urlopen('https://statdata.pgatour.com/r/010/2020/player_stats.json') as response:
          source = response.read()
      
      data = json.loads(source)
      
      for item in data['tournament']['players']:
          try:
              pid = item['pid']
              stats = item['stats']
              for stat in stats:
                  statId = stat['statId']
                  name = stat['name']
                  tValue = stat['tValue']
                  print(pid, statId, name, tValue)
           except Exception as e:
              print(e)
              print(item)
              break
      

      【讨论】:

      • 谢谢粉红色的spikyhairman - 一个额外的问题 - 我如何提取“tournamentNumber”:“010”并将其添加到第一列 - print(tournamentNumber, pid, statId, name, tValue) ?
      • 数据中只有一场锦标赛,所以print (data['tournament']['tournamentNumber'])for 循环之前
      • 感谢两个答案都正确抓取数据,我如何将数据放入数据框中?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-22
      • 1970-01-01
      • 2019-02-03
      • 2018-07-23
      • 2021-07-18
      • 2018-10-31
      相关资源
      最近更新 更多