Python 数据帧值,其中日期时间介于另一个数据帧中的两个日期之间
给定两个数据帧:Python 数据帧值,其中日期时间介于另一个数据帧中的两个日期之间,python,pandas,Python,Pandas,给定两个数据帧: print(df1) id status started ended 0 001 green 2014-09-04 01:00:00 2014-09-05 01:49:49 1 001 green 2014-09-05 01:49:50 2014-09-08 03:49:49 2 001 orange 2014-09-08 01:49:50
print(df1)
id status started ended
0 001 green 2014-09-04 01:00:00 2014-09-05 01:49:49
1 001 green 2014-09-05 01:49:50 2014-09-08 03:49:49
2 001 orange 2014-09-08 01:49:50 2014-09-18 23:49:49
3 002 green 2014-09-03 07:00:00 2014-09-09 02:59:49
4 002 red 2014-09-09 02:59:50 2014-09-11 02:39:29
5 002 red 2014-09-11 02:39:30 2014-09-15 01:00:00
print(df2)
id action action_datetime
0 001 process1 2014-09-07 01:49:49
1 001 process2 2014-09-08 01:49:52
2 001 process3 2014-09-09 10:00:00
3 002 process1 2014-09-08 11:01:29
4 002 process2 2014-09-09 06:59:49
在df2中,我想使用df2的id
和action\u datetime
从df1中查找相应的状态。i、 e.状态
对应
到df1开始和结束之间df1.id
=df2.id
和action\u datetime
的每个df2行:
id action action_datetime status
0 001 process1 2014-09-07 01:49:49 green
1 001 process2 2014-09-08 01:49:52 orange
2 001 process3 2014-09-09 10:00:00 orange
3 002 process1 2014-09-08 11:01:29 green
4 002 process2 2014-09-09 06:59:49 red
我想你可以查一下@jezrael,谢谢你是的,这是一个非常好的答案,别忘了upvote;)回答得很好,我投了赞成票。在执行searchsorted
之前,我使用了np.searchsorted
@jezrael一个简单的问题,如何首先确保df1.id=df2.id
?否则,仅当df1.id=df2.id