Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Converting a Dictionary to DataFrame in Python

I have a dictionary of a static structure:

Key: Key: Value`

I will need to record data a few extra keys deep to the same depth, so somewhat uniform.

Example Dictionary:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{
    "Emissions": {
        "305-1": [
            "2014_249989",
            "2015_339998",
            "2016_617957",
            "2017_827230"
        ],
        "305-2": [
            "2014_33163",
            "2015_64280",
            "2016_502748",
            "2017_675091"
        ],
    },
    "Effluents and Waste": {
        "306-1": [
            "2014_143.29",
            "2015_277.86",
            "2016_385.67",
            "2017_460.6"
        ],
        "306-2": "blah blah blah",
    }
}

I want a DataFrame of this structure:

Parent Key | Child Key | Child Value
Parent Key | Child Key | Child Value
Parent Key | Child Key | Child Value
Parent Key | Child Key | Child Value

Example Desired DataFrame:

Emissions | 305-1 | ["2014_249989", "2015_339998", "2016_617957", "2017_827230"]
Emissions | 305-2 | ["2014_33163", "2015_64280", "2016_502748", "2017_675091"]
Effluents and Waste| 306-1 | ["2014_249989", "2015_339998", "2016_617957", "2017_827230"]
Effluents and Waste | 306-2 | blah blah blah

Where all Child Values are either a list object of strings or a string object.


From researching I found pandas.DataFrame.from_dict(). However neither orient values help in my case. As it is intended for flat dictionaries.

I genuinely haven’t a clue on how to approach this. What a simple libraries may be needed etc.

Please let me know if there are further details/ nuances I could clarify.

>Solution :

Use:

import pandas as pd

data = {
    "Emissions": {
        "305-1": ["2014_249989", "2015_339998", "2016_617957", "2017_827230"],
        "305-2": ["2014_33163", "2015_64280", "2016_502748", "2017_675091"],
    },
    "Effluents and Waste": {
        "306-1": ["2014_143.29", "2015_277.86", "2016_385.67", "2017_460.6"],
        "306-2": "blah blah blah",
    }
}

data = [[key, ikey, value] for key, values in data.items() for ikey, value in values.items()]
res = pd.DataFrame(data)
print(res)

Output

                     0  ...                                                  2
0            Emissions  ...  [2014_249989, 2015_339998, 2016_617957, 2017_8...
1            Emissions  ...  [2014_33163, 2015_64280, 2016_502748, 2017_675...
2  Effluents and Waste  ...  [2014_143.29, 2015_277.86, 2016_385.67, 2017_4...
3  Effluents and Waste  ...                                     blah blah blah
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading