python - Nested dictionary to multiindex dataframe where dictionary keys are column labels -
Say I have a dictionary that looks like this:
dictionary = {'A'}
: {'A': [1,2,3,4,5], 'B': [6,7,8,9,1]}, 'B': {'A': [2,3,4, 5,6], 'B': [7,8,9,1,2]}}
And I want a data frame that looks like something :
ABABAAB 0 1 6 2 7 1 2 7 3 8 2 3 8 4 9 3 4 9 5 1 4 5 1 6 2
What Is there a convenient way to do this? If I try:
in [99]: Dataframe (dictionary) [99]: AB A [1, 2, 3, 4, 5] [2, 3, 4, 5] , 6] B [6, 7, 8, 9, 1] [7, 8, 9, 1, 2]
I get a dataframe where each element is a list. What do I need to do is a multi index where each level is shown in the nested decree as well as the keys corresponding to each element in the list and in the list as shown above. I think I can work a lot of raw solutions but I hope that can be a bit easier.
Pandu wants to value multi-value values as tuples, not nested dykes. The simplest thing is to change your format to the correct format before trying to pass it in the dotframe:
gt; & Gt; Corrections = {(external, inside): value for external, inner dictionary of dictionary, dictionary.iteritems () in internal dictionary, inDict.iteritems ()} & gt; & Gt; & Gt; ('A', 'A'): [1, 2, 3, 4, 5], ('A', 'B'): [6, 7, 8, 9, 1], ('B', ' A '): [2, 3, 4, 5, 6], (' B ',' B '): [7, 8, 9, 1, 2]}> gt; & Gt; & Gt; Pandas Data Frame (Correction) AB ABAB 0 1 6 2 7 1 2 7 3 8 2 3 8 4 9 3 4 9 5 1 4 5 1 6 2 [5 Rows x 4 Column]