首页 > 解决方案 > 如何在转换为 Dataframe 之前展平复杂的 json

问题描述

我正在尝试将复杂的 json 转换为 pandas 数据框,但是在转换它时遇到了问题。我可能不确定如何将复杂的 json 转换为 pandas 数据框。

在此处输入图像描述

我想显示数据框中的所有数据,这就是我获取它的方式,并且我正在共享我的 json 数据的格式,以便可以理解

{
  '1164223': {
    'fullSummaryLink': '/series/19014/scorecard/1164223/new-zealand-a-vs-india-a-1st-unofficial-odi-india-a-tour-of-nz-2018-19',
    'innings': {
      '1': {
        'batsmen': [
          {
            'name': 'HD Rutherford',
            'href': 'http://www.espncricinfo.com/ci/content/player/331375.html',
            'stats': {
              'runs': {
                'name': 'runs',
                'text': 'RUNS',
                'value': '70'
              },
              'ballsFaced': {
                'name': 'ballsFaced',
                'text': 'BF',
                'value': '66'
              },
              'notouts': {
                'name': 'notouts',
                'text': 'Not Out',
                'value': '0'
              }
            }
          },
          {
            'name': 'JDS Neesham',
            'href': 'http://www.espncricinfo.com/ci/content/player/355269.html',
            'stats': {
              'runs': {
                'name': 'runs',
                'text': 'RUNS',
                'value': '79'
              },
              'ballsFaced': {
                'name': 'ballsFaced',
                'text': 'BF',
                'value': '48'
              },
              'notouts': {
                'name': 'notouts',
                'text': 'Not Out',
                'value': '1'
              }
            }
          }
        ],
        'team': {
          'teamDisplayName': 'NEW ZEALAND A',
          'innDisplayName': 'INNINGS',
          'runs': 308,
          'overs': 50,
          'wickets': 6,
          'description': 'complete',
          'inningsRunWicket': '308/6',
          'inningStatus': ''
        },
        'bowlers': [
          {
            'name': 'S Kaul',
            'href': 'http://www.espncricinfo.com/ci/content/player/326017.html',
            'stats': {
              'overs': {
                'name': 'overs',
                'text': 'O',
                'value': '10'
              },
              'conceded': {
                'name': 'conceded',
                'text': 'R',
                'value': '74'
              },
              'wickets': {
                'name': 'wickets',
                'text': 'E',
                'value': '2'
              }
            }
          },
          {
            'name': 'K Gowtham',
            'href': 'http://www.espncricinfo.com/ci/content/player/424377.html',
            'stats': {
              'overs': {
                'name': 'overs',
                'text': 'O',
                'value': '9'
              },
              'conceded': {
                'name': 'conceded',
                'text': 'R',
                'value': '46'
              },
              'wickets': {
                'name': 'wickets',
                'text': 'E',
                'value': '1'
              }
            }
          }
        ]
      },
      '2': {
        'bowlers': [
          {
            'name': 'HK Bennett',
            'href': 'http://www.espncricinfo.com/ci/content/player/226493.html',
            'stats': {
              'overs': {
                'name': 'overs',
                'text': 'O',
                'value': '10'
              },
              'conceded': {
                'name': 'conceded',
                'text': 'R',
                'value': '65'
              },
              'wickets': {
                'name': 'wickets',
                'text': 'E',
                'value': '2'
              }
            }
          },
          {
            'name': 'LH Ferguson',
            'href': 'http://www.espncricinfo.com/ci/content/player/493773.html',
            'stats': {
              'overs': {
                'name': 'overs',
                'text': 'O',
                'value': '10'
              },
              'conceded': {
                'name': 'conceded',
                'text': 'R',
                'value': '75'
              },
              'wickets': {
                'name': 'wickets',
                'text': 'E',
                'value': '2'
              }
            }
          }
        ],
        'batsmen': [
          {
            'name': 'V Shankar',
            'href': 'http://www.espncricinfo.com/ci/content/player/477021.html',
            'stats': {
              'runs': {
                'name': 'runs',
                'text': 'RUNS',
                'value': '87'
              },
              'ballsFaced': {
                'name': 'ballsFaced',
                'text': 'BF',
                'value': '80'
              },
              'notouts': {
                'name': 'notouts',
                'text': 'Not Out',
                'value': '1'
              }
            }
          },
          {
            'name': 'SS Iyer',
            'href': 'http://www.espncricinfo.com/ci/content/player/642519.html',
            'stats': {
              'runs': {
                'name': 'runs',
                'text': 'RUNS',
                'value': '54'
              },
              'ballsFaced': {
                'name': 'ballsFaced',
                'text': 'BF',
                'value': '54'
              },
              'notouts': {
                'name': 'notouts',
                'text': 'Not Out',
                'value': '0'
              }
            }
          }
        ],
        'team': {
          'teamDisplayName': 'INDIA A',
          'innDisplayName': 'INNINGS',
          'runs': 311,
          'overs': 49,
          'wickets': 6,
          'description': 'target reached',
          'inningsRunWicket': '311/6',
          'inningStatus': ''
        }
      }
    },
    'isAvailable': True
  }
}

这是我的 python 代码,它将 json 值的字典转换为 pandas 数据框

df = pd.concat({k: pd.DataFrame(v) for k, v in scorecard_summary.items()})
df

标签: pythonjsonpandas

解决方案


推荐阅读