Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a nested dictionary to pandas dataframe?

I have a dictionary "my_dict" in this format:

{'l1':{'c1': {'a': 0, 'b': 1, 'c': 2},
       'c2': {'a': 3, 'b': 4, 'c': 5}},
 'l2':{'c1': {'a': 0, 'b': 1, 'c': 2},
       'c2': {'a': 3, 'b': 4, 'c': 5}}
}

Currently, I am using pd.DataFrame.from_dict(my_dict, orient='index') and get a df like this:

                             c2                           c1
l1  {u'a': 3, u'c': 5, u'b': 4}  {u'a': 0, u'c': 2, u'b': 1}
l2  {u'a': 3, u'c': 5, u'b': 4}  {u'a': 0, u'c': 2, u'b': 1}

However, what I want is both l1/l2 and c2/c3 as indexes and a/b/c as columns.
Something like this:

       a   b   c
l1 c1  0   1   2
   c2  3   4   5
l2 c1  0   1   2
   c2  3   4   5

What's the best way to do this?

like image 354
George Liu Avatar asked Nov 16 '16 17:11

George Liu


People also ask

How do you turn a dictionary into a pandas series?

You can create a pandas series from a dictionary by passing the dictionary to the command: pandas. Series() .

Can you put a dictionary in a pandas DataFrame?

A pandas DataFrame can be converted into a Python dictionary using the DataFrame instance method to_dict(). The output can be specified of various orientations using the parameter orient. In dictionary orientation, for each column of the DataFrame the column value is listed against the row label in a dictionary.

How do I get values from nested dictionaries?

Access Values using get() Another way to access value(s) in a nested dictionary ( employees ) is to use the dict. get() method. This method returns the value for a specified key. If the specified key does not exist, the get() method returns None (preventing a KeyError ).

Can DataFrame be created from dictionary?

Create dataframe with Pandas DataFrame constructor The dictionary below has two keys, scene and facade. Each value has an array of four elements, so it naturally fits into what you can think of as a table with 2 columns and 4 rows. Pandas is designed to work with row and column data. Each row has a row index.


1 Answers

Consider a dictionary comprehension to build a dictionary with tuple keys. Then, use pandas' MultiIndex.from_tuples. Below ast is used to rebuild you original dictionary from string (ignore the step on your end).

import pandas as pd
import ast

origDict = ast.literal_eval("""
{'l1':{'c1': {'a': 0, 'b': 1, 'c': 2},
       'c2': {'a': 3, 'b': 4, 'c': 5}},
 'l2':{'c1': {'a': 0, 'b': 1, 'c': 2},
       'c2': {'a': 3, 'b': 4, 'c': 5}}
}""")

# DICTIONARY COMPREHENSION
newdict = {(k1, k2):v2 for k1,v1 in origDict.items() \
                       for k2,v2 in origDict[k1].items()}
print(newdict)
# {('l1', 'c2'): {'c': 5, 'a': 3, 'b': 4},
#  ('l2', 'c1'): {'c': 2, 'a': 0, 'b': 1},
#  ('l1', 'c1'): {'c': 2, 'a': 0, 'b': 1},
#  ('l2', 'c2'): {'c': 5, 'a': 3, 'b': 4}}

# DATA FRAME ASSIGNMENT
df = pd.DataFrame([newdict[i] for i in sorted(newdict)],
                  index=pd.MultiIndex.from_tuples([i for i in sorted(newdict.keys())]))    
print(df)
#        a  b  c
# l1 c1  0  1  2
#    c2  3  4  5
# l2 c1  0  1  2
#    c2  3  4  5
like image 133
Parfait Avatar answered Sep 28 '22 06:09

Parfait