简体   繁体   中英

Creating pandas dataframe from accessing specific keys of nested dictionary

How can below dictionary converted to expected dataframe like below?

{
    "getArticleAttributesResponse": {
        "attributes": [{
            "articleId": {
                "id": "2345",
                "locale": "en_US"
            },
            "keyValuePairs": [{
                "key": "tags",
                "value": "[{\"displayName\": \"Nice\", \"englishName\": \"Pradeep\", \"refKey\": \"Key2\"}, {\"displayName\": \"Family Sharing\", \"englishName\": \"Sarvendra\", \"refKey\": \"Key1\", \"meta\": {\"customerDisplayable\": [false]}}}]"
            }]
        }]
    }
}

Expected dataframe:

    id           displayName              englistname          refKey
    2345            Nice                   Pradeep             Key2
    2345         Family Sharing            Sarvendra           Key1
df1 = pd.DataFrame(d['getDDResponse']['attributes']).explode('keyValuePairs')
df2 = pd.concat([df1[col].apply(pd.Series) for col in df1],1).assign(value = lambda x :x.value.apply(eval)).explode('value')
df = pd.concat([df2[col].apply(pd.Series) for col in df2],1)

OUTPUT:

      0     0     display englishName reference   source
0  1234  tags  Unarchived  Unarchived    friend  monster

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM