首页 > 解决方案 > 从 Pandas GroupBy 函数中提取结果

问题描述

我对 Python 比较陌生,并开始使用 Pandas。我查看了 Pandas 文档,但找不到我需要的东西,尽管这可能是由于不熟悉某些术语。

我使用了 Dataframe GroupBy 函数并得到了这些结果

mean_len = spam.groupby(spam.target).mean()

Function      Mean
   A          1.5
   B          2.6

我的问题是,什么代码会从 groupby 输出生成以下结果:

(1.5, 2.6)

多谢

标签: pythonpandaspandas-groupby

解决方案


You can extract the series Mean as a numpy array using

means = mean_len.Mean.values

If what you actually want is a list or a tuple, you can additionally do

tuple(means.tolist())

推荐阅读