pandas 带有 MultiIndex 的 DataFrame 来字典
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39067831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DataFrame with MultiIndex to dict
提问by OAK
I have a dataframe with a MultiIndex. I am wondering whether I created the data frame in the correct manner (see below).
我有一个带有 MultiIndex 的数据框。我想知道我是否以正确的方式创建了数据框(见下文)。
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:
df.index.values
结果是:
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:
df.index.get_level_values
结果是:
<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=[]
I am ultimately looking to transform the df into a dict of dictionaries such that the first dict key are one of ['bar','baz', 'foo','qux'] and values are the dates and the inner dictionary is made of 'total1' and 'totals2' as key and the values are the integers of the df. Alternative explanation, is for example if dict1 is the dict then calling:
我最终希望将 df 转换为字典的 dict,这样第一个 dict 键是 ['bar','baz', 'foo','qux'] 之一,值是日期和内部字典'total1' 和 'totals2' 作为键,值是 df 的整数。替代解释,例如,如果 dict1 是 dict 然后调用:
dict1['bar']
would result in the output:
将导致输出:
{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 would I need to alter in order to achieve this? Is this an indexing issue?
为了实现这一目标,我需要如何改变以及需要改变什么?这是索引问题吗?
回答by piRSquared
For converting whole dataframe to dictionary Try:
将整个数据帧转换为字典尝试:
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}}}
For converting one particular column, select before converting it to dictionary i.e
要转换某一特定列,请在将其转换为字典之前进行选择,即
df.groupby(level=0).apply(lambda df: df.xs(df.name)[colname].to_dict()).to_dict()