Create pandas dataframe from nested dict with outer keys as df index and inner keys column headers

maracuja

I have a nested dictionary like below

dictA = {X:{A: 0.2, B:0.3, C:0.4} ,Y:{A: 0.05, B:0.8, C:0.1},Z:{A: 0.15, B:0.6, C:0.25}}

I want to create a dataframe where the first key corresponds to the index and the keys of the nested dictionaries are the column headers. For example,

     A    B    C
  X  0.2  0.3  0.4 
  Y  0.05 0.8  0.1
  Z  0.15 0.6  0.25

I know I can pull out the keys, from the outer dict, into a list (using a list comprehension):

index_list = [key for key in dictA.iterkeys()]

and then the nested dictionaries into a single dictionary:

dict_list = [value for value in dictA.itervalues()]
final_dict = {k: v for dict in dict_list for k, v in dict.items()}

Finally I could create my df by:

df = pd.DataFrame(final_dict, index = index_list)

The problem is i need to map the correct values back to the correct index which is difficult when the ordinary of dictionary changes.

I imagine there is a completely different and more efficient way than what I have suggested above, help please?

Anand S Kumar

You can simply convert your dictA to a DataFrame and then take transpose, to make columns into index and index into columns. Example -

df = pd.DataFrame(dictA).T

Demo -

In [182]: dictA = {'X':{'A': 0.2, 'B':0.3, 'C':0.4} ,'Y':{'A': 0.05, 'B':0.8, 'C':0.1},'Z':{'A': 0.15, 'B':0.6, 'C':0.25}}

In [183]: df = pd.DataFrame(dictA).T

In [184]: df
Out[184]:
      A    B     C
X  0.20  0.3  0.40
Y  0.05  0.8  0.10
Z  0.15  0.6  0.25

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Create pandas dataframe from nested dict with outer keys as df index and inner keys column headers

From Dev

How to convert a nested dict to a dataframe, without converting the inner keys to columns

From Dev

Obtaining a pandas dataframe from a dict with tuples as keys

From Dev

Create a Dictionary from a Dataframe With the Index as the Keys

From Dev

Pandas Dataframe to_dict() with unique column values as keys

From Dev

getting only inner keys from a nested dictionary

From Dev

Pandas DataFrame to Dict Format with new Keys

From Dev

KeyError: Not in index, using a keys generated from a Pandas dataframe on itself

From Dev

nested dict python keys

From Dev

Unable to get the column keys from mysql into pandas dataframe

From Dev

Jq tsv - create headers from keys

From Dev

Create MultiIndex pandas DataFrame from dictionary with tuple keys

From Dev

Create pandas DataFrame from two lists with same keys

From Dev

How to set new columns in a multi-column index from a dict with partially specified tuple keys?

From Dev

How to set new columns in a multi-column index from a dict with partially specified tuple keys?

From Dev

Access dictionary keys and values in pandas dataframe column

From Dev

Aggregate a column by only some keys in Pandas dataframe?

From Dev

Create Nested Python Dictionary From Tuple of Keys

From Dev

Filter inner keys from 2 level nested dictionaries

From Dev

Filter inner keys from 2 level nested dictionaries

From Dev

Create pandas df from JSON where Column Headers and Rows are in separate arrays

From Java

Creating a Pandas DataFrame from a Numpy array: How do I specify the index column and column headers?

From Dev

Finding Merge (Outer - Inner) Pandas DF differences

From Dev

Assign table headers as keys in Python dict

From Java

Get list from pandas DataFrame column headers

From Dev

How can I print only the keys of inner dictionaries that are nested within a list, within a dict, in alphabetical order?

From Dev

Nested dictionary to multiindex dataframe where dictionary keys are column labels

From Dev

create a dict with unique keys and various list values from a tuple

From Dev

create a dict with unique keys and various list values from a tuple

Related Related

  1. 1

    Create pandas dataframe from nested dict with outer keys as df index and inner keys column headers

  2. 2

    How to convert a nested dict to a dataframe, without converting the inner keys to columns

  3. 3

    Obtaining a pandas dataframe from a dict with tuples as keys

  4. 4

    Create a Dictionary from a Dataframe With the Index as the Keys

  5. 5

    Pandas Dataframe to_dict() with unique column values as keys

  6. 6

    getting only inner keys from a nested dictionary

  7. 7

    Pandas DataFrame to Dict Format with new Keys

  8. 8

    KeyError: Not in index, using a keys generated from a Pandas dataframe on itself

  9. 9

    nested dict python keys

  10. 10

    Unable to get the column keys from mysql into pandas dataframe

  11. 11

    Jq tsv - create headers from keys

  12. 12

    Create MultiIndex pandas DataFrame from dictionary with tuple keys

  13. 13

    Create pandas DataFrame from two lists with same keys

  14. 14

    How to set new columns in a multi-column index from a dict with partially specified tuple keys?

  15. 15

    How to set new columns in a multi-column index from a dict with partially specified tuple keys?

  16. 16

    Access dictionary keys and values in pandas dataframe column

  17. 17

    Aggregate a column by only some keys in Pandas dataframe?

  18. 18

    Create Nested Python Dictionary From Tuple of Keys

  19. 19

    Filter inner keys from 2 level nested dictionaries

  20. 20

    Filter inner keys from 2 level nested dictionaries

  21. 21

    Create pandas df from JSON where Column Headers and Rows are in separate arrays

  22. 22

    Creating a Pandas DataFrame from a Numpy array: How do I specify the index column and column headers?

  23. 23

    Finding Merge (Outer - Inner) Pandas DF differences

  24. 24

    Assign table headers as keys in Python dict

  25. 25

    Get list from pandas DataFrame column headers

  26. 26

    How can I print only the keys of inner dictionaries that are nested within a list, within a dict, in alphabetical order?

  27. 27

    Nested dictionary to multiindex dataframe where dictionary keys are column labels

  28. 28

    create a dict with unique keys and various list values from a tuple

  29. 29

    create a dict with unique keys and various list values from a tuple

HotTag

Archive