我在用莎士比亚的语料库。
act literature_type scene scene_text scene_title speaker title
0 1 Comedy 1 In delivering my son from me, I bury a second ... Rousillon. The COUNT's palace. COUNTESS All's Well That Ends Well
1 1 Comedy 1 And I in going, madam, weep o'er my father's d... Rousillon. The COUNT's palace. BERTRAM All's Well That Ends Well
2 1 Comedy 1 You shall find of the king a husband, madam; y... Rousillon. The COUNT's palace. LAFEU All's Well That Ends Well
3 1 Comedy 1 What hope is there of his majesty's amendment? Rousillon. The COUNT's palace. COUNTESS All's Well That Ends Well
4 1 Comedy 1 He hath abandoned his physicians, madam; under... Rousillon. The COUNT's palace. LAFEU All's Well That Ends Well我想找到每个标题的平均scene_text长度。
我想用一些类似于:
all_works_by_speaker_df.groupby('title').apply(lambda x: np.mean(len(x)))这只会返回每个标题中的场景数。
发布于 2017-11-23 16:32:43
如果需要字符的len:
df = (all_works_by_speaker_df.groupby('title')['scene_text']
.apply(lambda x: np.mean(x.str.len()))
.reset_index(name='mean_len_text'))
print (df)
title mean_len_text
0 All's Well That Ends Well 48.4如果需要的话,使用len的单词,使用Vaishali's solution。
发布于 2017-11-23 16:35:22
从列中取字符串的长度,然后由一个数组组成,这是您的播放标题,然后应用平均值。
mean_len = df.scene_text.str.len().groupby(df.title).mean()发布于 2017-11-23 16:33:08
分裂,卑劣
df.groupby('title').scene_text.apply(lambda x: x.str.split().str.len().mean())
title
All's Well That Ends Well 9.2https://stackoverflow.com/questions/47459824
复制相似问题