有哪些方法可以合并时间戳不完全匹配的列?
DF1:
date start_time employee_id session_id
01/01/2016 01/01/2016 06:03:13 7261824 871631182
DF2:
date start_time employee_id session_id
01/01/2016 01/01/2016 06:03:37 7261824 871631182
我可以在 ['date', 'employee_id', 'session_id'] 加入,但有时同一个员工会在同一日期有多个相同的 session ,这会导致重复。我可以删除发生这种情况的行,但如果这样做,我会丢失有效 session 。
如果 DF1 的时间戳与 DF2 的时间戳相差 <5 分钟,并且 session_id 和 employee_id 也匹配,是否有一种有效的加入方式?如果有匹配的记录,那么时间戳总是比 DF1 稍晚,因为事件在未来某个时间点被触发。
['employee_id', 'session_id', 'timestamp<5minutes']
编辑 - 我以为之前有人会遇到这个问题。
我正在考虑这样做:
- 在每个数据帧上记录我的时间戳
- 创建一个时间戳 + 5 分钟(四舍五入)的列
- 创建一个时间戳列 - 5 分钟(四舍五入)
创建一个 10 分钟的间隔字符串以加入文件
df1['low_time'] = df1['start_time'] - timedelta(minutes=5) df1['high_time'] = df1['start_time'] + timedelta(minutes=5) df1['interval_string'] = df1['low_time'].astype(str) + df1['high_time'].astype(str)
有人知道如何将这 5 分钟间隔四舍五入到最接近的 5 分钟标记吗?
02:59:37 - 5 分钟 = 02:55:00
02:59:37 + 5 分钟 = 03:05:00
interval_string = '02:55:00-03:05:00'
pd.merge(df1, df2, how = 'left', on = ['employee_id', 'session_id', 'date', 'interval_string']
有谁知道如何这样计算时间?这似乎可行。你还是根据日期、员工、 session 来匹配,然后你找基本相同的10分钟间隔或范围内的时间
最佳答案
我会尝试在 pandas 中使用这个方法:
您感兴趣的参数是direction
、tolerance
、left_on
和right_on
建立@Igor 回答:
import pandas as pd
from pandas import read_csv
from io import StringIO
# datetime column (combination of date + start_time)
dtc = [['date', 'start_time']]
# index column (above combination)
ixc = 'date_start_time'
df1 = read_csv(StringIO(u'''
date,start_time,employee_id,session_id
01/01/2016,02:03:00,7261824,871631182
01/01/2016,06:03:00,7261824,871631183
01/01/2016,11:01:00,7261824,871631184
01/01/2016,14:01:00,7261824,871631185
'''), parse_dates=dtc)
df2 = read_csv(StringIO(u'''
date,start_time,employee_id,session_id
01/01/2016,02:03:00,7261824,871631182
01/01/2016,06:05:00,7261824,871631183
01/01/2016,11:04:00,7261824,871631184
01/01/2016,14:10:00,7261824,871631185
'''), parse_dates=dtc)
df1['date_start_time'] = pd.to_datetime(df1['date_start_time'])
df2['date_start_time'] = pd.to_datetime(df2['date_start_time'])
# converting this to the index so we can preserve the date_start_time columns so you can validate the merging logic
df1.index = df1['date_start_time']
df2.index = df2['date_start_time']
# the magic happens below, check the direction and tolerance arguments
tol = pd.Timedelta('5 minute')
pd.merge_asof(left=df1,right=df2,right_index=True,left_index=True,direction='nearest',tolerance=tol)
output
date_start_time date_start_time_x employee_id_x session_id_x date_start_time_y employee_id_y session_id_y
2016-01-01 02:03:00 2016-01-01 02:03:00 7261824 871631182 2016-01-01 02:03:00 7261824.0 871631182.0
2016-01-01 06:03:00 2016-01-01 06:03:00 7261824 871631183 2016-01-01 06:05:00 7261824.0 871631183.0
2016-01-01 11:01:00 2016-01-01 11:01:00 7261824 871631184 2016-01-01 11:04:00 7261824.0 871631184.0
2016-01-01 14:01:00 2016-01-01 14:01:00 7261824 871631185 NaT NaN NaN
关于python - 基于不完全匹配的时间戳的 Pandas 合并,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34880539/