开始使用免费开始使用

EDA 可视化 II

我们还可以想到,打车价格可能会在一天中发生变化。

您的目标是绘制一天中每个小时的中位数车费的折线图。hour 特征已为您计算好。如果您还不熟悉日期类特征的用法不用担心,我们会在"特征工程"一章中详细讲解。

本练习是课程的一部分

用 Python 赢下 Kaggle 竞赛

查看课程

练习说明

  • "hour"train DataFrame 分组,并计算 "fare_amount" 列的中位数。
  • 使用得到的 hour_price DataFrame,绘制以 "hour" 为 x 轴、"fare_amount" 为 y 轴的折线。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Create hour feature
train['pickup_datetime'] = pd.to_datetime(train.pickup_datetime)
train['hour'] = train.pickup_datetime.dt.hour

# Find median fare_amount for each hour
hour_price = train.____('____', as_index=False)['____'].____()

# Plot the line plot
plt.plot(hour_price[____], hour_price[____], marker='o')
plt.xlabel('Hour of the day')
plt.ylabel('Median fare amount')
plt.title('Fare amount based on day time')
plt.xticks(range(24))
plt.show()
编辑并运行代码