将pandas.core.groupby.SeriesGroupBy转换为dataframe

将pandas.core.groupby.SeriesGroupBy转换为dataframe

问题描述:

我有一个数据框,并应用了groupby方法。现在我有一个pandas.core.groupby.SeriesGroupBy,但我不能使用任何数据框方法。我怎样才能将它转换为可用的数据框?将pandas.core.groupby.SeriesGroupBy转换为dataframe

type(survivor) 
pandas.core.groupby.SeriesGroupBy 

应用.groups它看起来像这样:

{'C': Int64Index([ 1, 9, 19, 26, 30, 31, 34, 36, 39, 42, 
     847, 849, 852, 858, 859, 866, 874, 875, 879, 889], 
     dtype='int64', name=u'ID', length=168), 
'Q': Int64Index([ 5, 16, 22, 28, 32, 44, 46, 47, 82, 109, 116, 126, 
143, 
     156, 171, 186, 188, 196, 198, 208, 214, 241, 245, 260, 264, 274, 
     727, 749, 767, 768, 776, 778, 787, 790, 825, 828, 885, 890], 
     dtype='int64', name=u'ID'), 
'S': Int64Index([ 0, 2, 3, 4, 6, 7, 8, 10, 11, 12, 
     877, 878, 880, 881, 882, 883, 884, 886, 887, 888], 
     dtype='int64', name=u'ID', length=644)} 

我曾尝试以下的一些相关的其他问题的指示,但仍然得到同样的错误(例如): AttributeError的:“SeriesGroupBy '对象没有属性'set_index'

非常感谢!

它你seeem需要apply自定义函数:

def func(x): 
    #your code 
    return x 


survivor.apply(func)