开始使用免费开始使用

合并 DataFrame

在本练习中,您将把 riweather_rating 这两个 DataFrame 合并成一个新的 DataFrame,ri_weather

两个 DataFrame 将通过 ri 中的 stop_date 列与 weather_rating 中的 DATE 列进行连接。幸运的是,日期格式完全一致,但这并非总是如此!

合并完成后,您将把 stop_datetime 设为索引,该列是在上一个练习中保存的。

本练习是课程的一部分

使用 pandas 分析警务活动

查看课程

练习说明

  • 查看 ri DataFrame 的形状。
  • 使用左连接合并 riweather_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=____)
编辑并运行代码