converting pandas.core.groupby.SeriesGroupBy to dataframe

佐手、 提交于 2019-12-14 02:25:22

问题


I had a dataframe and I applied the groupby method. Now I have a pandas.core.groupby.SeriesGroupBy but I cant use any of the dataframe methods onto it. How can I convert it to a usable dataframe?

type(survivor)
pandas.core.groupby.SeriesGroupBy

by applying .groups it looks like this:

{'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)}

I have tried following the instructions of some other questions related but still get the same error(for example): AttributeError: 'SeriesGroupBy' object has no attribute 'set_index'

thanks a lot!


回答1:


You can convert the pandas.core.groupby.SeriesGroupBy to a DataFrame very simply as follows:

survivor.apply(pd.DataFrame)



回答2:


It seeem you need apply custom function:

def func(x):
    #your code
    return x


survivor.apply(func)


来源:https://stackoverflow.com/questions/46807334/converting-pandas-core-groupby-seriesgroupby-to-dataframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!