Pandas Groupby: Count and mean combined

6 浏览
0 Comments

Pandas Groupby: Count and mean combined

使用pandas来尝试总结数据框中特定类别的计数和这些类别的平均情感分数。

有一个包含不同情感分数的字符串表,我想通过统计每个文本来源的帖子数量以及这些帖子的平均情感来对它们进行分组。

我的(简化的)数据框如下所示:

source    text              sent
--------------------------------
bar       some string       0.13
foo       alt string        -0.8
bar       another str       0.7
foo       some text         -0.2
foo       more text         -0.5

输出应该类似于:

source    count     mean_sent
-----------------------------
foo       3         -0.5
bar       2         0.415

答案大致是:

df['sent'].groupby(df['source']).mean()

然而,这只提供了每个来源及其平均值,没有列标题。

0