从Pandas时间序列数据框中的多天中选择特定时间

时间:2019-10-29 17:48:05

标签: python pandas time-series

我如何在熊猫数据框中获取日期时间列(例如格式:2005-06-08 15:30:50),并在整个数据集中仅选择/标记特定时间(例如17:45:00) ?

我已经在多个网站上搜索了此答案,但没有找到解决整个数据帧中选择和标记特定时间戳的情况。

1 个答案:

答案 0 :(得分:1)

以下是示例:

print(df)

   amount           local_date
0     8.1  2016-09-30-17:45:00
1     4.0  2016-10-02-18:30:00
2     3.0  2016-10-03-17:45:00
3     9.7  2016-10-03-12:20:00
4    10.0  2016-10-04-01:20:32

df['local_date']=pd.to_datetime(df['local_date'])

df_filtered=df[df['local_date'].dt.time.apply(lambda x: x.strftime("%H:%M:%S")).eq('17:45:00')]
print(df_filtered)

   amount          local_date
0     8.1 2016-09-30 17:45:00
2     3.0 2016-10-03 17:45:00