根据另一个数据帧的日期范围进行过滤
我有两个 pandas 数据框,如下所示:
df1:
id date item
3 2015-11-23 B
3 2015-11-23 A
3 2016-05-11 C
3 2017-02-01 C
3 2018-07-12 E
4 2014-05-11 C
4 2015-02-01 C
4 2018-07-12 E
df2
id start end
3 2016-05-11 2017-08-30
4 2015-01-11 2017-08-22
我想剪切 df1 ,这样我只保留 df1 中属于df2 中给出的日期范围:
id date item
3 2016-05-11 C
3 2017-02-01 C
4 2015-02-01 C
实际上,df1 和 df2 有数百万行,因此,我无法使用 for 循环等进行任何快速修复。我大致了解如何使用 groupby by id,但恐怕到目前为止我所有的尝试都失败了。
先感谢您!
I have two pandas dataframes as following:
df1:
id date item
3 2015-11-23 B
3 2015-11-23 A
3 2016-05-11 C
3 2017-02-01 C
3 2018-07-12 E
4 2014-05-11 C
4 2015-02-01 C
4 2018-07-12 E
df2
id start end
3 2016-05-11 2017-08-30
4 2015-01-11 2017-08-22
I would like to cut df1 such that I only keep items of df1 which falls within the date ranges given in df2:
id date item
3 2016-05-11 C
3 2017-02-01 C
4 2015-02-01 C
In reality, df1 and df2 are of millions of rows and therefore, I won't be able to do any quick fixes using for loops for example. I have rough idea of using groupby by id, but I am afraid all my tries have failed so far.
Thank you in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
基本方法是构建一个包含该 id 的所有可能事件的数据帧。然后,您可以过滤该事件是否在两个日期之间。
The basic way is to build a dataframe containing all possible events for that
id
. You can then filter on whether that event is between your two dates.