開始使用免費開始

EDA 視覺化 II

另一個可以思考的方向是,乘車價格可能會隨著一天中的時段而變化。

你的目標是繪製一天中每個小時的車資中位數,做成一張簡單的折線圖。hour 特徵已經幫你算好。如果你還不熟悉如何處理日期型特徵也沒關係,我們會在特徵工程的章節再深入探討。

本練習屬於課程

用 Python 拿下 Kaggle 競賽

檢視課程

練習說明

  • "hour"train DataFrame 分組,並計算 "fare_amount" 欄位的中位數。
  • 利用取得的 hour_price DataFrame,繪製折線:x 軸放 "hour",y 軸放 "fare_amount"

動手互動練習

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

# 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()
編輯並執行程式碼