Pandas json_normalize produces confusing `KeyError` message?

后端 未结 3 618
北海茫月
北海茫月 2021-01-12 02:27

I\'m trying to convert a nested JSON to a Pandas dataframe. I\'ve been using json_normalize with success until I came across a certain JSON. I\'ve made a smalle

3条回答
  •  深忆病人
    2021-01-12 02:58

    In this case, I think you'd just use this:

    In [57]: json_normalize(data[0]['events'])
    Out[57]: 
      group  schedule.ID schedule.date schedule.location.building  \
    0     A          815    2015-08-27                        BDC   
    1     A          816    2015-08-27                        BDC   
    
       schedule.location.floor  
    0                        5  
    1                        5  
    

    The meta paths ([['schedule','date']...]) are for specifying data at the same level of nesting as your records, i.e. at the same level as 'events'. It doesn't look like json_normalize handles dicts with nested lists particularly well, so you may need to do some manual reshaping if your actual data is much more complicated.

提交回复
热议问题