比较 #python 与 #rstats
现在我们已经有了一个可以在多处检查某个单词是否出现在推文中的函数,接下来就可以把它应用到多个单词上并进行比较。让我们回到数据科学话题标签数据集的示例。我们想比较 #rstats 相对于 #python 出现了多少次。
数据科学话题标签数据集 ds_tweets 已为您加载。
本练习是课程的一部分
在 Python 中分析社交媒体数据
练习说明
- 使用函数
check_word_in_tweet()在ds_tweets的文本字段中查找所有#python的出现。 - 对
#rstats做同样的操作。 - 打印提到
#python的推文占比:用np.sum()对python求和,并将其除以ds_tweets.shape[0]。 - 对
rstats做同样的计算。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Find mentions of #python in all text fields
python = ____(____, ds_tweets)
# Find mentions of #rstats in all text fields
rstats = ____(____, ____)
# Print proportion of tweets mentioning #python
print("Proportion of #python tweets:", ____(____) / ____)
# Print proportion of tweets mentioning #rstats
print("Proportion of #rstats tweets:", ____(____) / ____)