创建时间序列数据框
当我们想要分析或探索随时间变化的情况时,会使用时间序列数据。在探索 Twitter 文本数据时,如果您想跟踪某个词或一组词的出现频率,这很有用。
第一步是将 DataFrame 转换为可以用 pandas 时间序列方法处理的格式。可以通过将索引转换为 datetime 类型来完成这一点。
本练习是课程的一部分
在 Python 中分析社交媒体数据
练习说明
- 使用
.head()方法打印ds_tweets中created_at的前 5 行。 - 使用 Pandas 的
.to_datetime()方法将该列转换为 datetime 类型。 - 再次打印前 5 行。
- 使用
.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.____(____)