如何将多个数组属性值获取到csv文件中

时间:2019-05-30 14:41:17

标签: python json pandas dataframe

我有以下示例数据。我的代码如下

代码:

样本数据:

{
  "data": [
  {
      "IdNo": "1234",
      "Name": "ABC",
       "Distribution": {
          "Section": [
            {
              "SectionName": {
                "id": "S1234"
              }
            }
          ],
          "Identity": [
            {
              "SectionName": {
                "id": "S5678"
              }
            },
            {
              "SectionName": {
                "id": "S7878"
              }
            }
          ]
          }

     }
     ]
}

df = pd.DataFrame()
for i in range(len(json_file['data'])):
    temp = {}
    temp['IdNo'] = json_file['data'][i]['IdNo']
    temp['Name'] = json_file['data'][i]['Name']
    for key in json_file['data'][i]['Distribution'].keys():
        try:
            for j in range(len(json_file['data'][i]['Distribution'][key])):
                temp[key]  = json_file['data'][i]['Distribution'][key][j]['SectionName']['id'] 
        except:
            temp[key] = None                    
        temp_df = pd.DataFrame([temp])
        df = pd.concat([df, temp_df], sort=True)        

我在输出中仅获得1条记录

IdNo     Name       Section     Identity
1234     Abc        S1234       S7878

请帮我任何建议,我如何才能达到以下输出 但我试图实现低于输出。

IdNo     Name           Section         Identity
1234     Abc            S1234           S5678
1234     Abc                            S7878

0 个答案:

没有答案