从一个数据帧与另一个数据帧查找值,然后根据条件是否满足在 df1 中创建新列
我有两个数据框。
- df1 有一个日期时间列,
- df2 有三列“开始时间”和“结束时间”以及一个名为“阶段编号”的列。
我正在尝试在df1数据帧中查找**日期时间**值,看看它是否在开始时间和结束时间列之间strong>df2,如果是这样,则使用 df2 数据帧中的阶段编号在 df1 中创建一个新列。
我可以使用迭代函数来做到这一点,但这对于大文件来说太耗时了。我还有什么其他方法可以做到这一点?
先感谢您
I have two data frames.
- df1 has a datetime column and
- df2 has three columns 'start time' and 'end time' alongwith a column called " stage number" .
I am trying to lookup **datetime **value in the df1 dataframe to see if it is between Start Time and end time columns in df2 and if that is true then create a new column in df1 with the stage number from the df2 dataframe.
I can do that with the iteration function but that is too time consuming for big files. What are other ways i can do this ?
Thank you in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将源数据帧(df1 和 df2)创建为:
以及
日期时间、开始时间和结束时间 列的类型为 datetime64[ns]。
请注意,df1 中的 2022-01-05 08:00:00 不包含在任何
df2 中的一对开始/结束时间。
在这种情况下(无阶段)阶段编号将设置为0。
首先计算 3 个辅助 Numpy 数组:
然后将新列计算为:
结果为:
I created source DataFrames (df1 and df2) as:
and
datetime, start time and end time columns are of datetime64[ns] type.
Note that 2022-01-05 08:00:00 in df1 is not contained between any
pair of start / end times in df2.
In such a case (no stage) the stage number will be set to 0.
Start with computing 3 auxiliary Numpy arrays:
Then compute the new column as:
The result is: