【发布时间】:2017-04-23 17:38:01
【问题描述】:
与 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()
但只给出每个来源,而且很平均,没有列标题。
【问题讨论】:
标签: python python-2.7 pandas dataframe group-by