開始使用免費開始

存取使用者自訂位置

在投影片中,我們看到可以透過使用者產生的文字來取得位置資訊,包括推文本身,以及使用者描述中的 location 欄位。這兩種方式的定位最不精確,但通常也最容易取得。

在這個練習中,你要從單一範例推文和一大組推文中擷取使用者自訂的位置。我們已經在 flatten_tweets() 函式中再加入一行,讓你能在資料框中存取使用者自訂的位置。

tweet_obj['user-location'] = tweet_obj['user']['location']

另外,單一推文的 JSON 物件 tweet_json,以及「國情咨文」相關推文的 JSON 物件 tweets_sotu_json 都已為你載入。

本練習屬於課程

使用 Python 分析社群媒體資料

檢視課程

練習說明

  • 印出 Twitter JSON 中單一推文的使用者自訂位置。
  • 將「國情咨文」推文的 Twitter JSON 展平成表格。
  • 使用 value_counts()head() 函式,印出 tweets_sotu 中前 10 個使用者自訂位置。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Print out the location of a single tweet
print(tweet_json[____][____])

# Flatten and load the SOTU tweets into a dataframe
tweets_sotu = pd.DataFrame(____(____))

# Print out top five user-defined locations
print(tweets_sotu[____].____().head())
編輯並執行程式碼