开始使用免费开始使用

比较 #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:", ____(____) / ____)
编辑并运行代码