gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 14:37:39 24 4
gpt4 key购买 nike

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

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”

非常感谢!

最佳答案

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

survivor.apply(pd.DataFrame)

关于python - 将 pandas.core.groupby.SeriesGroupBy 转换为 dataframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46807334/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com