统计词语

在 Python 中分析社交媒体数据

Alex Hanna

Computational Social Scientist

为什么要统计词语?

  • 文本分析自动化的基础步骤
  • 比较文档间相关关键词被提及的次数
  • 练习中:#rstats vs #python
在 Python 中分析社交媒体数据

用 str.contains 计数

  • str.contains
    • pandas Series 的字符串方法
    • 返回布尔型 Series
    • case = False:不区分大小写
在 Python 中分析社交媒体数据

公司数据集

import pandas as pd

tweets = pd.DataFrame(flatten_tweets(companies_json))
apple = tweets['text'].str.contains('apple', case = False)
print(np.sum(apple) / tweets.shape[0])
0.112
在 Python 中分析社交媒体数据

在多个文本字段中计数

apple = tweets['text'].str.contains('apple', 
                                     case = False) 
for column in ['extended_tweet-full_text',
    'retweeted_status-text',
    'retweeted_status-extended_tweet-full_text']:
    apple = apple | tweets[column].str.contains('apple', 
                                                 case = False)

print(np.sum(apple) / tweets.shape[0])
0.12866666666666668
在 Python 中分析社交媒体数据

Passons à la pratique !

在 Python 中分析社交媒体数据

Preparing Video For Download...