The open blogging platform. Say no to algorithms and paywalls.

Convert Nested Dictionary to pandas Dataframe

we have created 3 dictionaries inside a dictionary “countries”. We then assigned these dictionaries to the values of the respective keys, 1 and 2, and 3. In the pandas’ document, The “orientation” of…

Photo by Aaron Burden on Unsplash

A dictionary containing other dictionaries is called a nested dictionary.

Example:

countries = {
  1: { Country: "India", Capital: "Dehli", Population: "1,414,681,270" },
  2: {
    Country: "United State",
    Capital: "Washington, D.C.",
    Population: "335,962,130",
  },
  3: { Country: "China", Capital: "Beijing", Population: "1,453,523,041" },
}

we have created 3 dictionaries inside a dictionary “countries”. We then assigned these dictionaries to the values of the respective keys, 1 and 2, and 3. method1: Using pandas dataframe from a dictionary as given below

pd.DataFrame.from_dict(mydic)

In the pandas’ document, The “orientation” of the data. If the keys of the passed dict should be the columns of the resulting DataFrame, pass ‘columns’ (default) Otherwise, if the keys should be rows, pass ‘index’. So, We use the keyword argument orient as 'index' because keys 1 and 2, and 3 are associated with three dictionaries inside the dictionary are treated as rows. and key inside each dictionary as columns by default.

countries={"1":{"Country": "India",
     "Capital": "Dehli",
     "Population": "1,414,681,270"},
"2":{"Country": "United State",
     "Capital": "Washington, D.C.",
     "Population": "335,962,130"},
"3":{"Country": "China",
     "Capital": "Beijing",
     "Population": "1,453,523,041"}}

df =pd.DataFrame.from_dict(countries,orient='index')
df

Output:

        Country           Capital     Population
1         India             Dehli  1,414,681,270
2  United State  Washington, D.C.    335,962,130
3         China           Beijing  1,453,523,041

method 2:using pandas.dataframe, it converts each assigned key into columns and keys inside three dictionaries into an index:

countries={"1":{"Country": "India",
     "Capital": "Dehli",
     "Population": "1,414,681,270"},
"2":{"Country": "United State",
     "Capital": "Washington, D.C.",
     "Population": "335,962,130"},
"3":{"Country": "China",
     "Capital": "Beijing",
     "Population": "1,453,523,041"}}
df = pd.DataFrame(countries)
df

Output:

                        1                 2              3
Country             India      United State          China
Capital             Dehli  Washington, D.C.        Beijing
Population  1,414,681,270       335,962,130  1,453,523,041

while using the T property transpose the index and columns of the data frame,

countries={"1":{"Country": "India",
     "Capital": "Dehli",
     "Population": "1,414,681,270"},
"2":{"Country": "United State",
     "Capital": "Washington, D.C.",
     "Population": "335,962,130"},
"3":{"Country": "China",
     "Capital": "Beijing",
     "Population": "1,453,523,041"}}

df = pd.DataFrame(countries).T
df

Output:

        Country           Capital     Population
1         India             Dehli  1,414,681,270
2  United State  Washington, D.C.    335,962,130
3         China           Beijing  1,453,523,041

📚 For more information visit this ebook: Solved Exercises Of Python Dictionary




Continue Learning