python - How to import complex dict into pandas? -


i have data extracted xml file:

d = {     'test1': {         0: {'a': {'min': 1, 'mid': 12, 'max': 13},             'b': {'min': 2, 'mid': 22, 'max': 23}},         3: {'a': {'min': 3, 'mid': 32, 'max': 33},             'b': {'min': 4, 'mid': 42, 'max': 43}},         8: {'a': {'min': 5, 'mid': 52, 'max': 53},             'b': {'min': 6, 'mid': 62, 'max': 63}},                 },     'test2': {         0: {'a': {'min': 9, 'mid': 12, 'max': 13},             'b': {'min': 8, 'mid': 22, 'max': 23}},         3: {'a': {'min': 7, 'mid': 32, 'max': 33},             'b': {'min': 6, 'mid': 42, 'max': 43}},         8: {'a': {'min': 5, 'mid': 52, 'max': 53},             'b': {'min': 4, 'mid': 62, 'max': 63}},                }, } 

and imported with:

ds = pd.dataframe.from_dict(d) 

i name columns start browsing data.

the columns are: ['tests', 'id', 'item']

for example mid value tests, item , id. naively wrote:

ds[:,:,:,'min']  

but doesn't work.

also afraid dataframe not represented supposed to. make this?

+-------+----+------+-----+-----+-----+ |       |    |      | min | mid | max | +-------+----+------+-----+-----+-----+ | tests | id | item |     |     |     | +-------+----+------+-----+-----+-----+ |       | 0  |    |  1  | 12  | 13  | |       |    |  b   |  2  | 22  | 23  | + test1 +----+------+-----+-----+-----+ |       | 1  |    |  3  | 32  | 33  | |       |    |  b   |  4  | 42  | 43  | +-------+----+------+-----+-----+-----+ |       | 0  |    |  9  | 12  | 13  | |       |    |  b   |  8  | 22  | 23  | + test2 +----+------+-----+-----+-----+ |       | 1  |    |  7  | 32  | 33  | |       |    |  b   |  6  | 42  | 43  | +-------+----+------+-----+-----+-----+ 

you need manual transformation stack after reading them in (it may difficult done in 1 single step since in general dictionary can nested deep can):

(pd.dataframe.from_dict(d, orient="index")    .stack().apply(pd.series)    .stack().apply(pd.series)    .rename_axis(("tests", "id", "item"))) 

enter image description here


Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -