建立時間序列資料框
時間序列資料用於分析或探索隨時間變化的情況。當你想追蹤某個單字或一組單字在 Twitter 文字資料中的出現情形時,這特別實用。
第一步是把 DataFrame 轉換成可以用 pandas 的時間序列方法處理的格式。做法是把索引轉換為 datetime 型別。
本練習屬於課程
使用 Python 分析社群媒體資料
練習說明
- 在
ds_tweets中使用.head()方法,印出created_at的前五列。 - 使用 Pandas 的
.to_datetime()方法,將該欄位轉換為 datetime 型別。 - 再次印出前五列。
- 使用
.set_index()將索引設為created_at。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Print created_at to see the original format of datetime in Twitter data
print(ds_tweets[____].____())
# Convert the created_at column to np.datetime object
ds_tweets[____] = pd.____(____)
# Print created_at to see new format
print(ds_tweets[____].____())
# Set the index of ds_tweets to created_at
ds_tweets = ds_tweets.____(____)