DataFrame with MultiIndex to define

I have a dataframe with MultiIndex. I am wondering if I created the data frame correctly (see below).

01.01 02.01 03.01 04.01 bar total1 40 52 18 11 total2 36 85 5 92 baz total1 23 39 45 70 total2 50 49 51 65 foo total1 23 97 17 97 total2 64 56 94 45 qux total1 13 73 38 4 total2 80 8 61 50 

df.index.values results in:

 array([('bar', 'total1'), ('bar', 'total2'), ('baz', 'total1'), ('baz', 'total2'), ('foo', 'total1'), ('foo', 'total2'), ('qux', 'total1'), ('qux', 'total2')], dtype=object) 

df.index.get_level_values results in:

 <bound method MultiIndex.get_level_values of MultiIndex(levels=[[u'bar', u'baz', u'foo', u'qux'], [u'total1', u'total2']], labels=[[0, 0, 1, 1, 2, 2, 3, 3], [0, 1, 0, 1, 0, 1, 0, 1]],names=[] 

Ultimately, I want to convert df to a dictionary recorder, so the first key is one of ['bar', 'baz', 'foo', 'qux'], and the values ​​are dates and the internal dictionary consists of "total1" and "totalals2" as the key, and the values ​​are integers df. An alternative explanation, for example, if dict1 is a dict, then the call:

 dict1['bar'] 

will lead to the conclusion:

 {u'bar':{'01.01':{'total1':40,'total2':36},'02.01':{'total1':52,'total2':85},'03.01':{'total1':18,'total2':5},'04.01':{'total1':11,'total2':92} } } 

How and what do I need to change to achieve this? Is this an indexing issue? I am relatively new to pandas and dictionaries, so I hope you can talk to me.

+5
source share
1 answer

To convert an entire data frame to a Try dictionary:

 df.groupby(level=0).apply(lambda df: df.xs(df.name).to_dict()).to_dict() {'bar': {'01.01': {'total1': 40, 'total2': 36}, '02.01': {'total1': 52, 'total2': 85}, '03.01': {'total1': 18, 'total2': 5}, '04.01': {'total1': 11, 'total2': 92}}, 'baz': {'01.01': {'total1': 23, 'total2': 50}, '02.01': {'total1': 39, 'total2': 49}, '03.01': {'total1': 45, 'total2': 51}, '04.01': {'total1': 70, 'total2': 65}}, 'foo': {'01.01': {'total1': 23, 'total2': 64}, '02.01': {'total1': 97, 'total2': 56}, '03.01': {'total1': 17, 'total2': 94}, '04.01': {'total1': 97, 'total2': 45}}, 'qux': {'01.01': {'total1': 13, 'total2': 80}, '02.01': {'total1': 73, 'total2': 8}, '03.01': {'total1': 38, 'total2': 61}, '04.01': {'total1': 4, 'total2': 50}}} 

To convert one specific column, select before converting it to a dictionary ie

 df.groupby(level=0).apply(lambda df: df.xs(df.name)[colname].to_dict()).to_dict() 
+6
source

All Articles