且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

评估df每行中的日期时间函数是否在另一df中的日期时间范围内

更新时间:2023-10-06 12:54:52

让我重新说明您的问题。对于数据框 df_a 中的每一行,您要检查其在 df_a ['time'] 中的值是否在区间内由列 df_b ['date'] df_b ['date_new'] 中的值给出。如果是这样,则将 df_a [ id] 中的值设置为相应的 df_b [ id] 中的值

Let me rephrase your problem. For each row in dataframe df_a you want to check whether its value in df_a['time'] is in the interval given by the values in columns df_b['date'] and df_b['date_new']. If so, set the value in df_a["id"] as that in the corresponding df_b["id"].

如果这是您的问题,这是一个(非常粗糙的)解决方案:

If this is your question, this is a (very rough) solution:

for ia, ra in df_a.iterrows():
    for ib, rb in df_b.iterrows():
        if (ra["time"]>=rb['date']) & (ra["time"]<=rb['date_new']):
            df_a.loc[ia, "id"] = rb["id"]
            break