简体   繁体   中英

What can be the best approach for the following transformation?

I have a list of dictionaries as follows:

items = [
    {
        "identification":{
            "id":"ID1",
            "FCID":"FCID1"
        },
        "attributes":[
            {
                "key":"KEY1",
                "value":"VALUE11"
            },
            {
                "key":"KEY2",
                "value":"VALUE12"
            },
            {
                "key":"KEY3",
                "value":"VALUE13"
            }
        ]
    },
    {
        "identification":{
            "id":"ID2",
            "FCID":"FCID2"
        },
        "attributes":[
            {
                "key":"KEY1",
                "value":"VALUE21"
            },
            {
                "key":"KEY2",
                "value":"VALUE22"
            },
            {
                "key":"KEY3",
                "value":"VALUE23"
            },
            {
                "key":"KEY4",
                "value":"VALUE24"
            }
        ]
    }
]

Now the output I desire is: (in the form of Python DataFrame)

ID FCID  KEY1     KEY2    KEY3   KEY4
1   F1  VALUE11 VALUE12 VALUE13  None
2   F2  VALUE21 VALUE22 VALUE23 VALUE24

And I can't change the structure of it. That's the data I'm receiving from an API. So, how can I make this happen? Any Ideas

You can try using a list comprehension,

import pandas as pd

flatten = (
    {**{v['key']: v['value'] for v in i['attributes']}, **i['identification']} 
    for i in items
)

pd.DataFrame(flatten)

Out[*]: 

      KEY1     KEY2     KEY3   id   FCID     KEY4
0  VALUE11  VALUE12  VALUE13  ID1  FCID1      NaN
1  VALUE21  VALUE22  VALUE23  ID2  FCID2  VALUE24

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