LernerPython 博客,作者 Reuven Lerner:Pandas 日期范围选择:使用 .loc 对 datetime 索引进行切片
想从 Python Pandas 数据框中选择一段时间段吗?将 datetime 列设为索引,然后使用 .loc 和切片来选择日期:(
df
.set_index('tpep_pickup_datetime')
.sort_index()
.loc['2025-11-10':'2025-11-20']
['trip_distance']
.mean()
)