将pandas.core.groupby.SeriesGroupBy转换为dataframe [英] converting pandas.core.groupby.SeriesGroupBy to dataframe

查看:203
本文介绍了将pandas.core.groupby.SeriesGroupBy转换为dataframe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框,并应用了groupby方法.现在我有一个pandas.core.groupby.SeriesGroupBy,但是我不能在其上使用任何数据框方法.如何将其转换为可用的数据框?

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

通过应用.group看起来像这样:

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

我尝试按照其他一些相关问题的说明进行操作,但仍然遇到相同的错误(例如): AttributeError:"SeriesGroupBy"对象没有属性"set_index"

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'

非常感谢!

推荐答案

您可以非常简单地将pandas.core.groupby.SeriesGroupBy转换为DataFrame,如下所示:

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

survivor.apply(pd.DataFrame)

这篇关于将pandas.core.groupby.SeriesGroupBy转换为dataframe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆