如何将所有多值属性放入一个csv文件

时间:2019-05-30 10:49:24

标签: python json pandas dataframe multi-index

我有一个示例数据,如下。以下属性属于[data]字典。在“ XXXX”中,我的值是“ Naveen”,在“ YYYYY”中,我的值是“ Kumar”和“ Rajesh”。我正在尝试使用下面的代码来获取2条记录的输出

请提供任何建议

    {
  "data": [
  {
      "Empid": "1234",
      "Empname": "ABC",
       "data1": {
          "XXXX": [
            {
              "relative": {
                "id": "Naveen"
              }
            }
          ],
          "YYYYY": [
            {
              "relative": {
                "id": "Kumar"
              }
            },
            {
              "relative": {
                "id": "Rajesh"
              }
            }
          ]
          }

     }
     ]
}

请找到以下代码(我正在尝试)

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

我试图实现的最终输出

 EmpID EmpName XXXX   YYYYY 
 1234  ABC     Naveen  Kumar
 1234  ABC     Nan     Rajesh

但是我只得到1条记录

 EmpID EmpName XXXX   YYYYY 
 1234  ABC     Naveen  Kumar

如有任何建议,请帮助我

3 个答案:

答案 0 :(得分:0)

from pandas.io.json import json_normalize
key=list(json_file1['data'][0].keys())
df=pd.concat([json_normalize(json_file1['data'][0][key[0]]),json_normalize(json_file1['data'][0][key[1]])],axis=1)
df=df.ffill()
df.columns=key
df


   XXXX    YYYYY
0   Naveen  Kumar
1   Naveen  Rajesh

要使其自动化,您可以遍历不同的键

DF={}
key=list(json_file1['data'][0].keys())
for i in range(len(key)):
    df=json_normalize(json_file1['data'][0][key[i]])
    DF[key[i]]=(pd.concat([df],axis=0))
DF1=pd.concat(DF,axis=1)
DF1=DF1.ffill()
DF1.columns=key
DF1

输出

   XXXX   YYYYY    ZZZZZ
0   Naveen  Kumar   Kumar1
1   Naveen  Rajesh  Rajesh1

答案 1 :(得分:0)

如果您的json是这样的:

<a href="/physicalserverDetails?id=193" class="">CD-Z12</a>

下面的代码将结果放入矩阵json_file = { "data": [ { "XXXX": [ { "relative": { "id": "Naveen" } } ], "YYYYY": [ { "relative": { "id": "Kumar" } }, { "relative": { "id": "Rajesh" } } ] } ] } 中:

temp

结果是:

temp = []
for key in json_file['data'][0].keys():
    for i in json_file['data'][0][key]:
        temp.append([key,i['relative']['id']])

答案 2 :(得分:0)

DataFrame的另一个答案:

df = pd.DataFrame()
for key in json_file['data'][0].keys():
    for j in range(len(json_file['data'][0][key])):
        df.loc[j,key] = json_file['data'][0][key][j]['relative']['id']

结果:


XXXX    YYYYY
0   Naveen  Kumar
1   NaN Rajesh