我有一个数据框,df:
event
time
2013-10-09 06:27:00 breakfast
2013-10-09 07:25:00 school
2013-10-09 10:25:00 exam
2013-10-09 12:00:00 lunch
2013-10-09 12:00:00 homework
2013-10-09 19:20:00 dinner
2013-10-09 23:25:00 sleep
我还有两个相同大小的时间列表:
lower = array(['2013-10-09T06:15:00.000000000',
'2013-10-09T07:00:00.000000000',
'2013-10-19T13:00:00.000000000', dtype='datetime64[ns]')
upper = array(['2013-10-09T11:15:00.000000000',
'2013-10-09T12:00:00.000000000',
'2013-10-09T17:00:00.000000000', dtype='datetime64[ns]')
是否有一种非迭代的熊猫式方式可以让我获得df限制的行 [upper [0],lower [0]],[upper [1],lower [1]],[upper [2],lower [2]],...这样我们得到
event
time
2013-10-09 06:27:00 breakfast
2013-10-09 07:25:00 school
2013-10-09 10:25:00 exam
2013-10-09 07:25:00 school
2013-10-09 10:25:00 exam
2013-10-09 12:00:00 lunch
作为输出?
答案 0 :(得分:1)
一个非常熊猫式的(但可能没有记忆效率):
(
events.assign(key=1)
.merge(pd.DataFrame({"lower": lower, "upper": upper, "key": 1}), on="key")
[lambda df: df.time.between(d.lower, d.upper)]
[["time", "event"]]
.drop_duplicates()
)
答案 1 :(得分:0)
不是熊猫的功能,也许你想要什么?
[[u,l] for u, l in zip(upper, lower)]