Pandas - expand nested json array within column in dataframe

前端 未结 3 987
礼貌的吻别
礼貌的吻别 2021-02-05 17:17

I have a json data (coming from mongodb) containing thousands of records (so an array/list of json object) with a structure like the below one for each object:

{         


        
3条回答
  •  梦如初夏
    2021-02-05 17:34

    The following code is what you want. You can unroll the nested list using python's built in list function and passing that as a new dataframe. pd.DataFrame(list(json_dict['nested_col']))

    You might have to do several iterations of this, depending on how nested your data is.

    from pandas.io.json import json_normalize
    
    
    df= pd.concat([pd.DataFrame(json_dict), pd.DataFrame(list(json_dict['nested_array_to_expand']))], axis=1).drop('nested_array_to_expand', 1)
    

提交回复
热议问题