如何将对象的类型从“ pandas.core.groupby.generic.SeriesGroupBy”转换为“ pandas.core.series.Series”?

问题描述 投票:0回答:1

我有一个类型为“ pandas.core.groupby.generic.SeriesGroupBy”的变量,该变量是通过对熊猫数据帧的各个字段进行分组而得到的。但是,我想将该变量转换为工作正常但有很多错误的pandas系列。

这是我尝试过的代码:

w = data.groupby(['dt', 'b'])['w']
w = pd.Series(w)

[当我尝试运行此代码时,要花费很多时间来执行,并且还会产生很多错误。

我将获得如下熊猫系列:enter image description here

但是,我期望与此类似:enter image description here

还有其他方法可以将DataFrame的以下列分组并存储在pandas Series中:enter image description here

python-3.x pandas dataframe pandas-groupby series
1个回答
0
投票

Pandas groupby对象是可迭代的。使用列表推导,您可以提取分区的子系列。试试:

list_of_series = [s for _, s in data.groupby(['dt', 'b'])['w']]

[list_of_series是一个列表,应包含您想要的熊猫系列。

© www.soinside.com 2019 - 2024. All rights reserved.