处理 DateTime 列
pandas 默认会把日期时间列读取为 object 类型。以文本形式存储的日期不利于时间序列分析,因此需要将这些列转换为 datetime 数据类型。这样可以更灵活地处理 pandas 中的日期,并从中提取有用的特征。
另一个股票数据集(这次是 Apple)已加载为 apple。
本练习是课程的一部分
Python 中的异常检测
练习说明
- 将
apple的Date列转换为datetime列。 - 提取星期几,并存入名为
day_of_week的新列。 - 提取月份数字,并存入名为
month的新列。 - 提取每月的日期,并存入名为
day_of_month的新列。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Convert the Date column to DateTime
apple['Date'] = ____
# Create a column for the day of the week
apple['day_of_week'] = ____
# Create a column for the month
apple['month'] = ____
# Create a column for the day of the month
apple['day_of_month'] = ____
print(apple[['day_of_week', 'month', 'day_of_month']])