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="reco rds") 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:
I am looking to convert a data frame to a nested dictionary. I have tried: frame.to_dict(orient="reco
[{'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:
Do you already have the list/array of dictionaries?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.