【问题标题】:Flattening a dataframe with nested dictionary使用嵌套字典展平数据框
【发布时间】:2020-11-29 07:31:16
【问题描述】:
     [
      {
        "match_hometeam_score": "2 ",
        "match_awayteam_score": " 0",    
        "statistics": [
              {
                "type": "Ball Possession",
                "home": "70%",
                "away": "30%"
              },
              {
                "type": "Goal Attempts",
                "home": "6",
                "away": "3"
              },
              {
                "type": "Shots on Goal",
                "home": "4",
                "away": "1"
              },
              {
                "type": "Shots off Goal",
                "home": "1",
                "away": "2"
              },
              {
                "type": "Blocked Shots",
                "home": "1",
                "away": "0"
              },
              {
                "type": "Free Kicks",
                "home": "10",
                "away": "12"
              },
              {
                "type": "Corner Kicks",
                "home": "5",
                "away": "2"
              },
              {
                "type": "Offsides",
                "home": "2",
                "away": "1"
              },
              {
                "type": "Goalkeeper Saves",
                "home": "1",
                "away": "2"
              },
              {
                "type": "Fouls",
                "home": "11",
                "away": "9"
              },
              {
                "type": "Yellow Cards",
                "home": "2",
                "away": "0"
              },
              {
                "type": "Total Passes",
                "home": "657",
                "away": "272"
              },
              {
                "type": "Tackles",
                "home": "11",
                "away": "18"
              }
            ]
          },
          .....
        ]

Here 是我得到的一个 json 文件的小示例代码。我想要的是通过提取统计列中的值来展平它。

我试过了

flat_matches = pd.concat([all_matches.drop(['statistics'],axis=1),all_matches['statistics'].apply(pd.Series)], axis=1)

它以某种方式起作用,但不像我希望的那样。我想用列制作我的新df;

  1. 索引
  2. match_hometeam_score
  3. match_awayteam_score
  4. GoalAttempts_home
  5. GoalAttempts_away
  6. Shots_on_Goal_home
  7. Shots_on_Goal_away
  8. DangerousAttacks_home
  9. DangerousAttacks_away

CSV代码如下;

,match_hometeam_score,match_awayteam_score,统计 0,3,1,"[{'type': 'Ball Possession', 'home': '44%', 'away': '56%'}, {'type': 'Goal Attempts', 'home' : '15', 'away': '6'}, {'type': '射门得分', 'home': '5', 'away': '5'}, {'type': '射门得分Goal', 'home': '9', 'away': '1'}, {'type': '盖帽', 'home': '1', 'away': '0'}, {'type ':'角球','home':'3','away':'3'},{'type':'越位','home':'4','away':'2'}, {'type': 'Goalkeeper Saves', 'home': '4', 'away': '2'}, {'type': 'Fouls', 'home': '11', 'away': '10 '}, {'type': '黄牌', 'home': '2', 'away': '4'}, {'type': 'Total Passes', 'home': '382', 'away ': '503'}, {'type': 'Tackles', 'home': '13', 'away': '16'}, {'type': 'Attacks', 'home': '97', 'away': '136'}, {'type': 'Dangerous Attacks', 'home': '45', 'away': '63'}]" 1,1,2,"[{'type': 'Ball Possession', 'home': '61%', 'away': '39%'}, {'type': 'Goal Attempts', 'home' : '22', 'away': '12'}, {'type': '射门得分', 'home': '10', 'away': '7'}, {'type': '射门得分Goal', 'home': '6', 'away': '3'}, {'type': '盖帽', 'home': '6', 'away': '2'}, {'type ':'角球','home':'7','away':'2'},{'type':'越位','home':'0','away':'2'}, {'type': 'Goalkeeper Saves', 'home': '5', 'away': '9'}, {'type': 'Fouls', 'home': '12', 'away': '13 '}, {'type': '黄牌', 'home': '4', 'away': '4'}, {'type': 'Total Passes', 'home': '421', 'away ': '271'}, {'type': 'Tackles', 'home': '14', 'away': '24'}, {'type': 'Attacks', 'home': '97', 'away': '86'}, {'type': '危险攻击', 'home': '43', 'away': '46'}]" 2,1,2,"[{'type': 'Ball Possession', 'home': '48%', 'away': '52%'}, {'type': 'Goal Attempts', 'home' : '16', 'away': '14'}, {'type': '射门得分', 'home': '4', 'away': '6'}, {'type': '射门得分Goal', 'home': '6', 'away': '5'}, {'type': '盖帽', 'home': '6', 'away': '3'}, {'type ':'角球','home':'4','away':'4'},{'type':'越位','home':'2','away':'6'}, {'type': 'Goalkeeper Saves', 'home': '4', 'away': '3'}, {'type': 'Fouls', 'home': '11', 'away': '14 '}, {'type': '黄牌', 'home': '2', 'away': '7'}, {'type': 'Total Passes', 'home': '594', 'away ': '643'}, {'type': 'Tackles', 'home': '24', 'away': '16'}, {'type': 'Attacks', 'home': '144', 'away': '130'}, {'type': '危险攻击', 'home': '77', 'away': '36'}]"

非常感谢您的各种帮助!请告诉我如何将这个 json 数据集展平到同一级别。我是新手爱好者。如果我可以提高我的问题的质量,请随时给我提示。

RESULT I WOULD LIKE IS THIS;

【问题讨论】:

  • 请。在问题中粘贴json,以便我们复制!
  • @BrunoMello 我更新了我的问题。你现在更喜欢它还是你需要什么?请告诉我

标签: python pandas dictionary nested


【解决方案1】:

下面显示了如何转换数据框中的给定行。您需要迭代并创建如下数据框。

import json
import pandas as pd

sample_row = [{'type': 'Ball Possession', 'home': '44%', 'away': '56%'}, {'type': 'Goal Attempts', 'home': '15', 'away': '6'}, {'type': 'Shots on Goal', 'home': '5', 'away': '5'}, {'type': 'Shots off Goal', 'home': '9', 'away': '1'}, {'type': 'Blocked Shots', 'home': '1', 'away': '0'}, {'type': 'Corner Kicks', 'home': '3', 'away': '3'}, {'type': 'Offsides', 'home': '4', 'away': '2'}, {'type': 'Goalkeeper Saves', 'home': '4', 'away': '2'}, {'type': 'Fouls', 'home': '11', 'away': '10'}, {'type': 'Yellow Cards', 'home': '2', 'away': '4'}, {'type': 'Total Passes', 'home': '382', 'away': '503'}, {'type': 'Tackles', 'home': '13', 'away': '16'}, {'type': 'Attacks', 'home': '97', 'away': '136'}, {'type': 'Dangerous Attacks', 'home': '45', 'away': '63'}]

js = json.dumps(sample_row)
df = pd.json_normalize(json.loads(js))

df['match_hometeam_score'] = [3] * len(df)
df['match_awayteam_score'] = [1] * len(df)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-18
    • 1970-01-01
    • 2019-11-14
    • 2019-11-15
    • 2012-09-06
    • 2019-03-03
    • 2020-09-06
    相关资源
    最近更新 更多