DataFrameをマージする
この演習では、ri と weather_rating の2つのDataFrameをマージして、新しいDataFrame ri_weather を作成します。
結合には、ri の stop_date 列と weather_rating の DATE 列を使います。幸い、日付のフォーマットは完全に一致しています(いつもそうとは限りません)。
マージが完了したら、前の演習で保存した列である stop_datetime をインデックスに設定します。
この演習はコースの一部です
pandasで警察活動を分析する
演習の手順
riDataFrame の shape を確認します。riとweather_ratingを left join でマージします。ri_weatherの shape を確認し、列が2つ増え、行数はriと同じであることを確かめます。ri_weatherのインデックスをstop_datetime列に置き換えます。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
# 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=____)