計算單字

使用 Python 分析社群媒體資料

Alex Hanna

Computational Social Scientist

為什麼要計數?

  • 自動化文字分析的基本步驟
  • 可比較文件中相關關鍵字被提及的次數
  • 在練習中:#rstats vs #python
使用 Python 分析社群媒體資料

用 str.contains 來計數

  • str.contains
    • pandas Series 的字串方法
    • 回傳布林 Series
    • case = False:不分大小寫搜尋
使用 Python 分析社群媒體資料

Companies 資料集

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 分析社群媒體資料

一起來練習吧!

使用 Python 分析社群媒體資料

Preparing Video For Download...