Link to home
Start Free TrialLog in
Avatar of AndyC1000
AndyC1000

asked on

Data frame to nested dictionary using to_dict

Dear all,

I am looking to convert a data frame to a nested dictionary.  I have tried: frame.to_dict(orient="records") which produces:

[{'count': 17479.0, 'std': 3.8812136764221474, 'min': 2.7599999999999998, 'max': 19.399999999999999, range2: 6.0, range1: 4.1600000000000001, range3: 11.1, 'mean': 7.54730877052463}, {'count': 17480.0, 'std': 2.4854229546254998, 'min': -1.1399999999999999, 'max': 11.19, range2: 2.8100000000000001, range1: 1.74, range3: 5.4624999999999995, 'mean': 3.5781401601830658},
{'count': 17479.0, 'std': 4.5011555793947053, 'min': 2.7917999999999998, 'max': 21.8095, range2: 6.5936000000000003, range1: 4.5523999999999996, range3: 12.26465, 'mean': 8.411080,9142399443}]                                                

Filename1, etc. should be the first key prior to count, mean etc. i.e:

[{'Filename1: {'count': 17479.0, 'std': 3.8812136764221474, 'min': 2.7599999999999998, 'max': 19.399999999999999, range2: 6.0, range1: 4.1600000000000001, range3: 11.1, 'mean': 7.54730877052463}},                                                
{Filename2: {'count': 17480.0, 'std': 2.4854229546254998, 'min': -1.1399999999999999, 'max': 11.19, range2: 2.8100000000000001, range1: 1.74, range3: 5.4624999999999995, 'mean': 3.5781401601830658}},
{Filename3: {'count': 17479.0, 'std': 4.5011555793947053, 'min': 2.7917999999999998, 'max': 21.8095, range2: 6.5936000000000003, range1: 4.5523999999999996, range3: 12.26465, 'mean': 8.411080,9142399443}}]            

I think it has something to do with the index.  frame.index.values returns
['Filename1', 'Filename2', 'Filename3']

Thanks

See data frame structure below:

User generated image
Avatar of aikimark
aikimark
Flag of United States of America image

Do you already have the list/array of dictionaries?
ASKER CERTIFIED SOLUTION
Avatar of gelonida
gelonida
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial