Python Pandas通过日期列表选择行 [英] Python pandas select rows by list of dates

查看:108
本文介绍了Python Pandas通过日期列表选择行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过日期列表选择数据框的多行

How to select multiple rows of a dataframe by list of dates

dates = pd.date_range('20130101', periods=6)
df = pd.DataFrame(np.random.randn(6,4), index=dates, columns=list('ABCD'))

In[1]: df

Out[1]: 
                   A         B         C         D
2013-01-01  0.084393 -2.460860 -0.118468  0.543618
2013-01-02 -0.024358 -1.012406 -0.222457  1.906462
2013-01-03 -0.305999 -0.858261  0.320587  0.302837
2013-01-04  0.527321  0.425767 -0.994142  0.556027
2013-01-05  0.411410 -1.810460 -1.172034 -1.142847
2013-01-06 -0.969854  0.469045 -0.042532  0.699582

myDates = ["2013-01-02", "2013-01-04", "2013-01-06"]

所以输出应该是

                   A         B         C         D
2013-01-02 -0.024358 -1.012406 -0.222457  1.906462
2013-01-04  0.527321  0.425767 -0.994142  0.556027
2013-01-06 -0.969854  0.469045 -0.042532  0.699582

推荐答案

您可以使用index.isin()方法创建用于子集的逻辑索引:

You can use index.isin() method to create a logical index for subsetting:

df[df.index.isin(myDates)]

这篇关于Python Pandas通过日期列表选择行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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