尋找關鍵字
計算已知關鍵字,是你在分析 Twitter 資料集中的文字資料時最先會用到的方法之一。在這個資料集中,你要在一批與 data science 相關的推文裡,計算特定主題標籤出現的次數。為此,你會使用 pandas Series 物件的字串方法來完成。
pandas 和 numpy 分別已匯入為 pd 與 np。功能更完整的 flatten_tweets 和 data_science_json 也已為你載入。
本練習屬於課程
使用 Python 分析社群媒體資料
練習說明
- 使用
flatten_tweets()攤平推文並存成flat_tweets。 - 使用 pandas 的 DataFrame 建構子將推文轉為 DataFrame。
- 在
'text'欄位中尋找#python的出現,忽略大小寫。 - 列印提到
#python的推文比例:用np.sum()對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:", ____ / ____)