熊猫:Groupby改进

时间:2016-10-11 19:59:54

标签: python pandas dataframe multi-index

任何人都有加速或消除for循环的技巧,仍然会出现与数据帧相同的元组键控字典?

import pandas as pd

foo1 = (['LABEL1','LABEL1','LABEL2','LABEL2'])
foo2 = ([5,5,6,6])
foo3 = ([1,1,2,3])

index = pd.MultiIndex.from_arrays([foo1,foo2,foo3], 
                                  names=['ifoo1','ifoo2','ifoo3'])

initFrame = pd.DataFrame({'bar1': [5,6,5,6],
                          'bar2': ['a','b','c','d'],
                          'bar3': [11,22,33,44],
                          'bar4': [1,2,1,3]}, index=index)

finDict = {}
grouper = initFrame.groupby(level=('ifoo2', 'ifoo3'))
for name, group in grouper:
    finDict.update({name:group}) 

0 个答案:

没有答案