Home > other >  how to convert list of dicts into dataframe
how to convert list of dicts into dataframe

Time:10-28

list_of_dict = [
    {
        '@id': '54', 
        '@name': '009de580ae2a20ad.jpg', 
        '@width': '1080', 
        '@height': '720', 
        'box': {
            '@label': 'Television', 
            '@occluded': '0', 
            '@source': 'manual', 
            '@xtl': '130.40833715734814', 
            '@ytl': '148.36237211407877', 
            '@xbr': '171.88589300809082', 
            '@ybr': '225.55914893617017', 
            '@z_order': '0'
        }
    }, 
    {
        '@id': '53', 
        '@name': '008f7096b1917873.jpg', 
        '@width': '1080', 
        '@height': '720', 
        'box': [
            {
                '@label': 'Ball', 
                '@occluded': '0', 
                '@source': 'manual', 
                '@xtl': '142.5709551986475', 
                '@ytl': '340.4156088727931', 
                '@xbr': '160.269078613694', 
                '@ybr': '369.9085559076505', 
                '@z_order': '0'
            }, 
            {
                '@label': 'Ball', 
                '@occluded': '0', 
                '@source': 'manual', 
                '@xtl': '128.40823088998914', 
                '@ytl': '55.6182888184699', 
                '@xbr': '149.14524815843498', 
                '@ybr': '100.27719330013579', 
                '@z_order': '0'
            }, 
            {
                '@label': 'Ball', 
                '@occluded': '0', 
                '@source': 'manual', 
                '@xtl': '82.38818017147688', 
                '@ytl': '0.005866908103214124', 
                '@xbr': '112.22427243086584', 
                '@ybr': '43.825803531009505', 
                '@z_order': '0'
            }
        ]
    }, 
    {
        '@id': '52', 
        '@name': '008d4f07e70a3a71.jpg', 
        '@width': '1080', 
        '@height': '720', 
        'box': {
            '@label': 'Ball', 
            '@occluded': '0', 
            '@source': 'manual', 
            '@xtl': '68.81703658978385', 
            '@ytl': '22.3059846084201', 
            '@xbr': '85.00099504890713', 
            '@ybr': '46.741656858306925', 
            '@z_order': '0'
        }
    }
]

convert this list of dicts to dataframe? can anyone send the possible answers

   @id                 @name @width @height  box.@label box.@occluded  \
0   59  00a3c8ba34448111.jpg   1080     720        Bird             0   
1   58  00a2fa166f338907.jpg   1080     720        Bird             0   
2   57  00a0793a49ea232b.jpg   1080     720        Bird             0   
3   56  00a00bb929a41617.jpg   1080     720        Bird             0   
4   55  009e6695349f0ca6.jpg   1080     720        Ball             0   
5   54  009de580ae2a20ad.jpg   1080     720  Television             0   
6   53  008f7096b1917873.jpg   1080     720         NaN           NaN

   box.@source             box.@xtl            box.@ytl            box.@xbr  \
0       manual    76.87732399468662   182.8539248528746  221.00822122932013   
1       manual    69.87695205893009  24.095391579900408  316.86542688081147   
2       manual   22.649090689530247   88.50817564508829  247.66808839512137   
3       manual   19.677101799299603   37.27246717971933  359.52614418548484   
4       manual   236.25551020408162   384.8457039384337  260.53144789276655   
5       manual   130.40833715734814  148.36237211407877  171.88589300809082   
6          NaN                  NaN                 NaN                 NaN 

CodePudding user response:

If you just want to make a dataframe, with box containing more than one dict, you can just use

df = pd.DataFrame.from_records(list_of_dict)

you will get this datadframe

A cleaner solution would be to create a row in the dataset for each dict inside the @box. To do so you would have to change the structure of each dict inside the list removing the @box key, making each dict having only one box using new keys like box_label and box_occupied.

CodePudding user response:

If you want to get 3 different rows for the second element in your list_of_dict, you'd first need to flatten the dicts

import pandas as pd

flatten = [
    {
        **{k: v for k, v in x.items() if k != "box"},
        **{f"box.{k}": v for k, v in box.items()},
    }
    for x in list_of_dict
    for box in (x["box"] if isinstance(x["box"], list) else [x["box"]])
]
df = pd.DataFrame(flatten))

to get

  @id                 @name @width  ...            box.@xbr            box.@ybr box.@z_order
0  54  009de580ae2a20ad.jpg   1080  ...  171.88589300809082  225.55914893617017            0
1  53  008f7096b1917873.jpg   1080  ...    160.269078613694   369.9085559076505            0
2  53  008f7096b1917873.jpg   1080  ...  149.14524815843498  100.27719330013579            0
3  53  008f7096b1917873.jpg   1080  ...  112.22427243086584  43.825803531009505            0
4  52  008d4f07e70a3a71.jpg   1080  ...   85.00099504890713  46.741656858306925            0
  • Related