展平包含列表的词典字典

时间:2018-08-03 06:58:07

标签: python dictionary dataframe flatten

我有一个字典字典,像这样:

data={'data': 'input',
 'test': 
 {
    'and': 
    {
    'range': {'month': [{'start': 'Jan','end': 'July'}]},
   'Student': {'Name': ['ABC'], 'Class': ['10']}
     }
  }
}

我需要将这个字典扁平化为一个数据框。我试图使用json_normalize()来扁平化字典,我得到的输出看起来像这样: enter image description here

我想要的输出类似于下面给出的输出。

enter image description here

这可以通过使用 as.data.frame(unlist(data))在R中完成,但是我想在 Python 中进行相同的拼合。我是python的新手,所以我对此并不了解。

1 个答案:

答案 0 :(得分:0)

我试图通过编写如下的递归函数来规范化您的json对象:

data={'data': 'input',
 'test': 
 {
    'and': 
    {
    'range': {'month': [{'start': 'Jan','end': 'July'}]},
   'Student': {'Name': ['ABC'], 'Class': ['10']}
     }
  }
}
sequence = ""
subDicts = []
def findAllSubDicts(data):
    global subDicts
    global sequence
    for key, value in data.items():
        sequence += key
        #print(sequence)
        if isinstance(value, str):
            subDicts.append([sequence,value])
            sequence = sequence[:sequence.rfind(".")+1]
            #print(sequence)
        elif isinstance(value, dict):
            tempSequence = sequence[:sequence.rfind(".")+1]
            sequence += "."
            #print(sequence)
            findAllSubDicts(value)
            sequence = tempSequence
        elif isinstance(value, list) and isinstance(value[0], dict):
                sequence += "."
                tempSequence = sequence[:sequence.rfind(".")+1]
                #print(sequence)
                findAllSubDicts(value[0])
                sequence = tempSequence
        elif isinstance(value, list) and len(value)==1:
            tempSequence = sequence[:sequence.rfind(".")+1]
            subDicts.append([sequence,value[0]])
            sequence = tempSequence
    return subDicts



outDict = findAllSubDicts(data)
for i in outDict:
    print(i[0].ljust(40," "), end=" ")
    print(i[1])

打印结果将为您提供:

data                                     input
test.and.range.month.start               Jan
test.and.range.month.end                 July
test.and.Student.Name                    ABC
test.and.Student.Class                   10

如果您需要对我的代码进行任何澄清或修改,请通知我。