合并 DataFrame
在本练习中,您将把 ri 和 weather_rating 这两个 DataFrame 合并成一个新的 DataFrame,ri_weather。
两个 DataFrame 将通过 ri 中的 stop_date 列与 weather_rating 中的 DATE 列进行连接。幸运的是,日期格式完全一致,但这并非总是如此!
合并完成后,您将把 stop_datetime 设为索引,该列是在上一个练习中保存的。
本练习是课程的一部分
使用 pandas 分析警务活动
练习说明
- 查看
riDataFrame 的形状。 - 使用左连接合并
ri和weather_rating两个 DataFrame。 - 查看
ri_weather的形状,确认它比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=____)