合併 DataFrame
在本練習中,你將把 ri 和 weather_rating 兩個 DataFrame 合併成新的 DataFrame:ri_weather。
這兩個 DataFrame 會用 ri 的 stop_date 欄位與 weather_rating 的 DATE 欄位進行連接。很幸運地,日期格式完全相同,但實務上並不總是如此!
合併完成後,你會把上一題已經存好的 stop_datetime 設為索引。
本練習屬於課程
使用 pandas 分析警方執法活動
練習說明
- 檢視
riDataFrame 的形狀(shape)。 - 以 left join 合併
ri與weather_rating兩個 DataFrame。 - 檢視
ri_weather的形狀,確認它比ri多 2 個欄位,但列數相同。 - 用
stop_datetime欄位取代ri_weather的索引。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Examine the shape of 'ri'
print(____)
# Merge 'ri' and 'weather_rating' using a left join
ri_weather = pd.merge(left=____, right=____, left_on='stop_date', right_on='DATE', how='____')
# Examine the shape of 'ri_weather'
print(____)
# Set 'stop_datetime' as the index of 'ri_weather'
ri_weather.set_index('____', inplace=____)