如何从嵌套字典制作DataFrame?

时间:2018-09-16 06:38:43

标签: json python-3.x dictionary pandas-groupby

我有json格式的数据。数据如下所示。

{   "8844250093": 
        {    
            "Name": "Sonu", 
            "Mobile": "1245787963", 
            "Date": "01/10/2017",  
            "Email": ""
        }, 
    "6583832455": 
        {    
            "Name": "yakub", 
            "Mobile": "3265982154", 
            "Date": "02/18/2017", 
            "SMSDetails": 
                       {  "sms1":
                               {   "body":"dsgfdag",
                                   "phone":"875"
                               },
                           "sms2":
                               {    "body":"hfgfyd",
                                     "phone":"565"
                                }
                       }, 
            "Email": ""
        },
   "659832582": 
        {    
            "Name": "sameer", 
            "Mobile": "5825982154", 
            "Date": "07/16/2018", 
            "SMSDetails": {"sms1":{"body":"lkjhgf","phone":"965"},
                           "sms2": {"body":"lpou","phone":"2354"}}, 
            "Email": ""
        }
}

我希望数据看起来像。最近几天我一直在尝试它,但是没能做到。

S.Number    key         MobileNumber    SMSbody  SMSphone   Date
1           8844250093  1245787963      NAN      NAN        01/10/2017
2           6583832455  3265982154      dsgfdag  875        02/18/2017
3           6583832455  3265982154      hfgfyd   565        02/18/2017
4           659832582   5825982154      lkjhgf   965        07/16/2018
5           659832582   5825982154      lpou     2354       07/16/2018

1 个答案:

答案 0 :(得分:1)

# d is Your dictionary
l = []
for i in d.keys():
    if 'SMSDetails' not in d[i].keys():
        x = dict(zip(d[i].keys(),d[i].values()))
        l.append(x)
    else:
        temp = d[i]['SMSDetails']
        for j in temp:
            t = {
                'Name':d[i]['Name'],
                'Mobile':d[i]['Mobile'],
                'Date':d[i]['Date'],
                'Email':d[i]['Email'],
                'SMSBody':temp[j]['body'],
                'SMSPhone':temp[j]['phone']
                }

            l.append(t)
pd.DataFrame(l)

输出:

        Date    Email   Mobile  Name    SMSBody SMSPhone
0   01/10/2017      1245787963  Sonu    NaN      NaN
1   02/18/2017      3265982154  yakub   dsgfdag 875
2   02/18/2017      3265982154  yakub   hfgfyd  565
3   07/16/2018      5825982154  sameer  lkjhgf  965
4   07/16/2018      5825982154  sameer  lpou    2354

编辑:您可以删除不需要的列,并根据需要对列重新排序。

希望有帮助!