查找关键词
在 Twitter 数据集中,统计已知关键词是分析文本数据的常用起点。在本数据集中,您将统计与数据科学相关的一组推文中,特定话题标签出现的次数。为此,您将使用 pandas Series 对象中的字符串方法来完成。
pandas 和 numpy 已分别以 pd 和 np 导入。功能更完整的 flatten_tweets 函数和 data_science_json 也已为您加载。
本练习是课程的一部分
在 Python 中分析社交媒体数据
练习说明
- 使用
flatten_tweets()扁平化推文,并将结果存入flat_tweets。 - 使用 pandas 的 DataFrame 构造函数将推文转换为 DataFrame。
- 在
'text'列中查找包含#python的推文,忽略大小写。 - 通过用
np.sum()对python求和,并除以推文总数,打印提到#python的推文所占比例。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Flatten the tweets and store them
____ = ____(____)
# Convert to DataFrame
ds_tweets = ____.____(____)
# Find mentions of #python in 'text'
python = ____[____].____.____(____, ____)
# Print proportion of tweets mentioning #python
print("Proportion of #python tweets:", ____ / ____)