Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何访问数据帧中的一行嵌套字典_Python_Pandas_List_Dictionary - Fatal编程技术网

Python 如何访问数据帧中的一行嵌套字典

Python 如何访问数据帧中的一行嵌套字典,python,pandas,list,dictionary,Python,Pandas,List,Dictionary,我有一个json文件,看起来像这样 { "file": "name", "main": [{ "question_no": "Q.1", "question": "what is ?", "answer": [{ "user": "John", "comment": "It is defin

我有一个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\u规范化(文件[“main”],记录\u path='answer',meta='question\u 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\u value
中的值,以将
my\u值和value\u 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
列的位置不是问题。

您也可以这样做:

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,我非常抱歉。就连我都是在发帖后才注意到的。现在json被更正了。再一次,我真的很抱歉。这是更好的解决方案感谢@NYC Coder,并非常感谢@jezrael为帮助我所做的努力。。非常感谢你。帮我节省了很多时间。我实际上是在用for循环等等。