Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting meta values from multiple level with json_normalize

Tags:

python

pandas

Suppose this is my JSON:

ds = [{
        "name": "groupa",
        "subGroups": [{
            "subGroup": 1,
            "people": [{
                "firstname":"Tony",
            },
            {
                "firstname":"Brian"
            }
            ]
        }]
    },
    {
        "name": "groupb",
        "subGroups": [{
            "subGroup": 1,
            "people": [{
                "firstname":"Tony",
            },
            {
                "firstname":"Brian"
            }
            ]
        }]
    }
]

I create a Dataframe by doing:

df = json_normalize(ds, record_path =['subGroups', 'people'], meta=['name'])

This gives me:

    firstname   name
0   Tony    groupa
1   Brian   groupa
2   Tony    groupb
3   Brian   groupb

However, I'd want to also include the subGroup column.

I try:

df = json_normalize(ds, record_path =['subGroups', 'people'], meta=['name', 'subGroup'])

But that gives:

KeyError: 'subGroup'

Any ideas?

like image 901
More Than Five Avatar asked Mar 21 '18 16:03

More Than Five


2 Answers

json_normalize(
   ds, 
   record_path=['subGroups', 'people'], 
   meta=[
           'name', 
           ['subGroups', 'subGroup']   # each meta field needs its own path
   ], 
   errors='ignore'
)

  firstname    name  subGroups.subGroup
0      Tony  groupa                   1
1     Brian  groupa                   1
2      Tony  groupb                   1
3     Brian  groupb                   1
like image 84
cs95 Avatar answered Nov 16 '22 02:11

cs95


Try this.

df = json_normalize(ds, record_path =['subGroups', 'people'],meta['name'['subGroups', 'subGroup']])
like image 1
Hayat Avatar answered Nov 16 '22 03:11

Hayat