从一个数据帧与另一个数据帧查找值,然后根据条件是否满足在 df1 中创建新列

发布于 2025-01-17 04:45:39 字数 333 浏览 3 评论 0原文

我有两个数据框。

  • 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋日私语 2025-01-24 04:45:39

我将源数据帧(df1df2)创建为:

             datetime
0 2022-01-03 10:30:00
1 2022-01-03 11:20:00
2 2022-01-05 12:00:00
3 2022-01-05 08:00:00
4 2022-01-06 16:05:00

以及

           start time            end time  stage number
0 2022-01-03 10:00:00 2022-01-03 12:00:00             1
1 2022-01-04 10:00:00 2022-01-04 16:00:00             2
2 2022-01-05 10:00:00 2022-01-06 18:00:00             3
3 2022-01-10 10:00:00 2022-01-20 18:00:00             4

日期时间开始时间结束时间 列的类型为 datetime64[ns]

请注意,df1 中的 2022-01-05 08:00:00 不包含在任何
df2 中的一对开始/结束时间。
在这种情况下(无阶段)阶段编号将设置为0

首先计算 3 个辅助 Numpy 数组:

st = df2['start time'].values
et = df2['end time'].values
dat = df1.datetime.values[:,None]

然后将新列计算为:

df1['stage'] = np.dot(np.logical_and(np.less_equal(st, dat),
    np.less_equal(dat, et)), df2['stage number'])

结果为:

             datetime  stage
0 2022-01-03 10:30:00      1
1 2022-01-03 11:20:00      1
2 2022-01-05 12:00:00      3
3 2022-01-05 08:00:00      0
4 2022-01-06 16:05:00      3

I created source DataFrames (df1 and df2) as:

             datetime
0 2022-01-03 10:30:00
1 2022-01-03 11:20:00
2 2022-01-05 12:00:00
3 2022-01-05 08:00:00
4 2022-01-06 16:05:00

and

           start time            end time  stage number
0 2022-01-03 10:00:00 2022-01-03 12:00:00             1
1 2022-01-04 10:00:00 2022-01-04 16:00:00             2
2 2022-01-05 10:00:00 2022-01-06 18:00:00             3
3 2022-01-10 10:00:00 2022-01-20 18:00:00             4

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:

st = df2['start time'].values
et = df2['end time'].values
dat = df1.datetime.values[:,None]

Then compute the new column as:

df1['stage'] = np.dot(np.logical_and(np.less_equal(st, dat),
    np.less_equal(dat, et)), df2['stage number'])

The result is:

             datetime  stage
0 2022-01-03 10:30:00      1
1 2022-01-03 11:20:00      1
2 2022-01-05 12:00:00      3
3 2022-01-05 08:00:00      0
4 2022-01-06 16:05:00      3
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文