简体   繁体   English

从 Pandas GroupBy function 中提取结果

[英]Extracting results from Pandas GroupBy function

I am relatively new to Python and started to use Pandas.我对 Python 比较陌生,并开始使用 Pandas。 I've looked at the Pandas documentation but can't see to find what I need although that could be due to being unfamiliar with some of the terminology.我查看了 Pandas 文档,但找不到我需要的东西,尽管这可能是由于不熟悉某些术语。

I used the Dataframe GroupBy function and have these results我使用了 Dataframe GroupBy function 并得到了这些结果

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

Function      Mean
   A          1.5
   B          2.6

My question is, what code would generate the result below from the groupby output:我的问题是,什么代码会从 groupby output 生成以下结果:

(1.5, 2.6)

thanks a lot多谢

This should convert it to a tuple:这应该将其转换为元组:

df = pd.DataFrame({
    'Function': ['A', 'B'],
    'Mean': [1.5, 2.5]
})
a = tuple(list(df['Mean']))
print(a)

(1.5, 2.5)

You can extract the series Mean as a numpy array using您可以将系列均值提取为 numpy 数组

means = mean_len.Mean.values

If what you actually want is a list or a tuple, you can additionally do如果你真正想要的是一个列表或一个元组,你还可以做

tuple(means.tolist())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM