Jonathon Hill
Jonathon Hill

Reputation: 1057

dict of dict to pandas dataframe

I have a dict of dict following the format:

{ row_name : { column_name : column_value } }

I want to convert this dictionary into a pandas dataframe:

row_name1 | column_name1.1  | column_name1.2   | column_name1.3   |
---------------------------------------------------------------------
row_name1 | column_value1.1 | column_value1.2  | column_value1.3  |
---------------------------------------------------------------------
row_name2 | column_name2.1  | column_name2.2   | column_name2.3   |
---------------------------------------------------------------------
row_name2 | column_value2.1 | column_value2.2  | column_value 2.3 |

Or something similar. Thanks

Upvotes: 2

Views: 131

Answers (1)

Marius
Marius

Reputation: 60060

DataFrame.from_dict does this, in your case the orient argument needs to be 'index' rather than 'columns' since you have row keys in the outer dictionary:

dd = {'rowA': {'colA': 1, 'colB': 2},
      'rowB': {'colA': 3, 'colB': 4}}
df = pd.DataFrame.from_dict(dd, orient='index')
df
Out[10]: 
      colB  colA
rowA     2     1
rowB     4     3

Upvotes: 3

Related Questions