How To Get Average Of Same Word More Than X Time Per Group?
How can I get the average of same word more than 4 times per group ? In other words: I want to calculate for each group (name) the number of sentences which contains same word at l
Solution 1:
First count words if same values >=
per sentences
by Counter
and then aggregate mean
:
from collections import Counter
df['avg'] = df['sentences'].apply(lambda x: sum(v>=4 for k,v in Counter(x.split()).items()))
df = df.groupby('name', as_index=False)['avg'].mean()
print (df)
name avg
0 aa 0.5
1 bb 0.0
2 cc 0.0
3 dd 1.0
Post a Comment for "How To Get Average Of Same Word More Than X Time Per Group?"