通过datetime.date列表选择数据框行(datetimeindex) [英] select dataframe rows (datetimeindex) by a list of datetime.date

查看:401
本文介绍了通过datetime.date列表选择数据框行(datetimeindex)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

df看起来像这样:

DateTime
2017-07-10 03:00:00    288.0
2017-07-10 04:00:00    306.0
2017-08-10 05:00:00    393.0
2017-08-10 06:00:00    522.0
2017-09-10 07:00:00    487.0
2017-09-10 08:00:00    523.0
2017-10-10 09:00:00    585.0

问题如何选择日期列表中的行:

Question how to select row that in a list of dates:

['2017-07-10', '2017-09-10']

拥有:

DateTime
2017-07-10 03:00:00    288.0
2017-07-10 04:00:00    306.0
2017-09-10 07:00:00    487.0
2017-09-10 08:00:00    523.0

谢谢

推荐答案

假设Datetime是索引,请尝试以下操作:

Assuming the Datetime is index, try with the below:

to_search=['2017-07-10', '2017-09-10']
df[df.index.to_series().dt.date.astype(str).isin(to_search)]


                        1
DateTime                  
2017-07-10 03:00:00  288.0
2017-07-10 04:00:00  306.0
2017-09-10 07:00:00  487.0
2017-09-10 08:00:00  523.0

这篇关于通过datetime.date列表选择数据框行(datetimeindex)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆