【问题标题】:How to access a nested dictionary which is a row in a Dataframe如何访问嵌套字典,它是数据框中的一行
【发布时间】:2020-09-15 18:41:31
【问题描述】:

我有一个像这样的 json 文件

{
    "file": "name",
    "main": [{
                "question_no": "Q.1",
                "question": "what is ?",
                "answer": [{
                        "user": "John",
                        "comment": "It is defined as",
                        "value": {
                            "numbers": 2,
                            "submitted_value": [{
                                    "time": "4:06",
                                    "my_value": {
                                        "value1": 5,
                                        "value2": 10
                                    }
                                },
                                {
                                    "time": "13:47",
                                    "my_value": {
                                        "value1": 24,
                                        "value2": 30
                                    }
                                }
                            ]
                        }

                    },
                    {
                        "user": "Sam",
                        "comment": "as John said above it simply means",
                        "value": {
                            "numbers": 2,
                            "submitted_value": [{
                                    "time": "6:08",
                                    "my_value": {
                                        "value1": 9,
                                        "value2": 10
                                    }
                                },
                                {
                                    "time": "15:24",
                                    "my_value": {
                                        "value1": 54,
                                        "value2": 19
                                    }
                                }
                            ]
                        },
                        "closed": "no"
                    }
                ]

            }]
            }

当我做data = pd.json_normalize(file["main"], record_path='answer', meta='question_no') 我得到的结果是

   user                             comment    question_no           value                     
0  John                    It is defined as       Q.1       [{'my_value': 5, 'value_2': 10}, {'my_value': 24, 'value_2': 30}]
1   Sam  as John said above it simply means       Q.1        [{'my_value': 9, 'value_2': 10}, {'my_value': 54, 'value_2': 19}]

我需要访问列表 value 和字典 submitted_value 中的值,以将 my_value and value_2 的总和作为新列。实际文件有点大,所以请考虑处理总和所花费的时间。

想要的结果:

value1_sum      value2_sum     question_no              user      comment  
 29                40                Q.1                    john    It is defined as
 63                29                Q.1                     Sam     as John said above it simply means

列的位置不是问题。

【问题讨论】:

  • 您的 JSON 是否正确? “提交”不应该在那里。

标签: python pandas list dictionary


【解决方案1】:

您也可以这样做:

with open('1.json', 'r+') as f:
    data = json.load(f)

df = pd.json_normalize(data['main'],
                       record_path=['answer', 'value', 'submitted_value'],
                       meta=[['question_no'], ['answer', 'user'], ['answer', 'comment']])
df = df.groupby(by=['answer.user', 'question_no', 'answer.comment'], as_index=False).sum()
print(df)

  answer.user question_no                      answer.comment  my_value.value1  my_value.value2
0        John         Q.1                    It is defined as               29               40
1         Sam         Q.1  as John said above it simply means               63               29

【讨论】:

  • 对于无效的 JSON,我深表歉意。甚至我只是在发布后才注意到它。现在 json 已更正。再次对此我感到非常抱歉。
  • 这是更好的解决方案
  • 感谢@NYC Coder,非常感谢@jezrael 为我提供的帮助。非常感谢。帮我节省了很多时间。我实际上是在使用 for 循环和所有。
猜你喜欢
  • 2020-08-28
  • 2021-01-03
  • 1970-01-01
  • 2016-07-10
  • 1970-01-01
  • 1970-01-01
  • 2020-10-03
  • 2020-10-01
  • 2013-10-06
相关资源
最近更新 更多