如何获得每组连续X次以上相同单词的平均值?

如何获得每组 X 次以上相同单词的平均值?

但在这里,我想连续获得每组(group = name)相同单词超过 4 次的平均值。

例子:

id | name | sentences

---------------------

1  |  aa  | david hi david david david

2  |  aa  | david david is at home

3  |  bb  | I'm king

4  |  cc  | where r u going

5  |  dd  | lol lol lol lol lol lol

6  |  ee  | abc abc cc abc abc abc abc cc

7  |  ee  | dd dd dd ee dd dd dd

我想得到以下结果:


name | avg

----------

aa   |  0.0   (0 sentence contain the words 'david' continuously 4 times in ). total instances of 'aa' group is 2

bb   |  0.0   (0 sentence contains same word continuously 4 times) 

cc   |  0.0   (0 sentence contains same word continuously 4 times)

dd   |  1.0   (1 sentence contains same word 'lol' continuously 4 times). total instances of 'dd' group is 1

ee   |  0.5   (1 sentence contains same word 'abc' continuously 4 times). total instances of 'dd' group is 2



I'm using python 3.6.8


喵喵时光机
浏览 80回答 1
1回答

汪汪一只猫

您可以4使用以下方法对连续出现的单词或连续多次进行计数Series.str.count,然后使用Series.groupby对系列cnt进行分组name并使用聚合mean来获得分组平均值。cnt = df['sentences'].str.count(r'(\w+)(\s\1){3,}')avg = cnt.groupby(df['name']).mean().reset_index(name='avg')细节:print(cnt)0    01    02    03    04    15    16    0Name: sentences, dtype: int64print(avg)  name  avg0   aa  0.01   bb  0.02   cc  0.03   dd  1.04   ee  0.5
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python