Python 评估df每行中的日期时间函数是否在另一df中的日期时间范围内
我是python新手,需要一些关于日期时间函数的问题的帮助 我有Python 评估df每行中的日期时间函数是否在另一df中的日期时间范围内,python,pandas,datetime,for-loop,series,Python,Pandas,Datetime,For Loop,Series,我是python新手,需要一些关于日期时间函数的问题的帮助 我有dfu a,它有一个标题为time的列,我正试图在此dfu a中创建一个新列id 我希望id列通过时间是否包含在df_b列的“日期”和“日期新”之间的时间范围内来确定,例如,第一行的日期为“2019-01-07 20:52:41”,而“日期新”为“2019-01-07 21:07:41”(15分钟的时间间隔),我想要这一行的索引,当时间为“2019-01-07 20:56:30”(即id=0)时,在df_a中显示为我的id,以此类推
dfu a
,它有一个标题为time
的列,我正试图在此dfu a
中创建一个新列id
我希望id
列通过时间是否包含在df_b
列的“日期”和“日期新”之间的时间范围内来确定,例如,第一行的日期为“2019-01-07 20:52:41”,而“日期新”为“2019-01-07 21:07:41”(15分钟的时间间隔),我想要这一行的索引,当时间为“2019-01-07 20:56:30”(即id=0)时,在df_a
中显示为我的id,以此类推,用于df_a
中的所有行
这个问题很相似,但我不知道如何让它与我的工作,因为我一直在努力
ValueError:无法处理非唯一索引
需要注意的是,df_b中的范围并不总是唯一的,这意味着某些间隔包含相同的时间段,在这些情况下,如果它使用它所属的df_b中第一个时间段的id,则可以忽略,此外,df_b中有200多行,df_a中有2000行,因此,以for循环类型格式定义每个时间段需要很长时间,除非有比定义每个时间段更简单的方法,请提前感谢您的帮助!如果这可以使用任何澄清,请让我知道
df_a
time id
2019-01-07 22:02:56 NaN
2019-01-07 21:57:12 NaN
2019-01-08 09:35:30 NaN
df_b
date date_new id
2019-01-07 21:50:56 2019-01-07 22:05:56 0
2019-01-08 09:30:30 2019-01-08 09:45:30 1
Expected Result
df_a
time id
2019-01-07 22:02:56 0
2019-01-07 21:57:12 0
2019-01-08 09:35:30 1
让我重新解释一下你的问题。对于数据帧
df_a
中的每一行,您要检查df_a['time']
中的值是否在df_b['date']
和df_b['date_new']列中的值给定的间隔内。如果是,请将df_a[“id”]
中的值设置为相应的df_b[“id”]
中的值
如果这是您的问题,这是一个(非常粗略的)解决方案:
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
df_a.iterrows()中ia、ra的
对于ib,df_b.ItErrors()中的rb:
如果(ra[“time”]>=rb['date'])和(ra[“time”]pandas对非相等联接没有很好的支持,这正是您所寻找的,但它确实有一个函数merge\u asof
,您可能想签出该函数:
这将大大加快您的加入速度
例如:
df_a = pd.DataFrame({'time': ['2019-01-07 22:02:56', '2019-01-07 21:57:12', '2019-01-08 09:35:30']})
df_b = pd.DataFrame({'date': ['2019-01-07 21:50:56', '2019-01-08 09:30:30'], 'date_new': ['2019-01-07 22:05:56', '2019-01-08 09:45:30'], 'id':[0,1]})
df_a['time'] = pd.to_datetime(df_a['time'])
df_b['date'] = pd.to_datetime(df_b['date'])
df_b['date_new'] = pd.to_datetime(df_b['date_new'])
#you need to sort df_a first before using merge_asof
df_a.sort_values('time',inplace=True)
result = pd.merge_asof(df_a, df_b, left_on='time', right_on='date')
#get rid of rows where df_a.time values are greater than df_b's new date
result = result[result.date_new > result.time]
请提供您的数据帧和预期结果的示例。请注意,df_a[“time”]
中的最后一个值在预期结果中有不同的值(2019-01-08与2019-01-07)。感谢您的更正,是的,它应该是01-08嘿,非常感谢您的帮助!这很有效,但它确实需要很长时间才能运行
df_a = pd.DataFrame({'time': ['2019-01-07 22:02:56', '2019-01-07 21:57:12', '2019-01-08 09:35:30']})
df_b = pd.DataFrame({'date': ['2019-01-07 21:50:56', '2019-01-08 09:30:30'], 'date_new': ['2019-01-07 22:05:56', '2019-01-08 09:45:30'], 'id':[0,1]})
df_a['time'] = pd.to_datetime(df_a['time'])
df_b['date'] = pd.to_datetime(df_b['date'])
df_b['date_new'] = pd.to_datetime(df_b['date_new'])
#you need to sort df_a first before using merge_asof
df_a.sort_values('time',inplace=True)
result = pd.merge_asof(df_a, df_b, left_on='time', right_on='date')
#get rid of rows where df_a.time values are greater than df_b's new date
result = result[result.date_new > result.time]