Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 基于时间差的Pandas中两个数据帧的合并_Python_Pandas_Numpy - Fatal编程技术网

Python 基于时间差的Pandas中两个数据帧的合并

Python 基于时间差的Pandas中两个数据帧的合并,python,pandas,numpy,Python,Pandas,Numpy,我有两个数据帧,df1,df2 df1: df2: 我想合并userId上的两个数据帧,以及时间范围列,比如说,时间范围为10分钟。属于df1 所以我想要的数据帧是这样的: dateTime userId session clickTime clickId 2018-08-30 02:20:19 2233 1 2018-08-30 02:21:09 1987 2018-08-30 02:20:1

我有两个数据帧,
df1
df2

df1:

df2:

我想合并
userId
上的两个数据帧,以及时间范围列,比如说,时间范围为10分钟。属于
df1

所以我想要的数据帧是这样的:

  dateTime               userId  session   clickTime             clickId

2018-08-30 02:20:19      2233      1       2018-08-30 02:21:09    1987
2018-08-30 02:20:19      2233      1       2018-08-30 02:23:19    1988
2018-08-30 02:20:19      2233      1       2018-08-30 02:21:09    1989
2018-08-30 02:20:19      2233      1       2018-08-30 02:21:09    1990
因此,我希望每个用户都使用它们,数据帧应该是这样的,对于每个
userId
,我希望使用这个数据帧。可能吗

因此,就像我想在
userId
上合并
df1
df2
一样,
df2
clickTime
应该在
df1
dateTime
列的10-15分钟的时间范围内,IIUC:Use


可以指定距离的公差

pd.merge_asof(
    df1, df2,
    left_on='dateTime',
    right_on='clickTime',
    by='userId',
    direction='nearest',
    tolerance=pd.Timedelta(15, unit='m')
)

             dateTime  userId  session           clickTime  clickId
0 2018-08-30 02:20:19    2233        1 2018-08-30 02:21:09   1987.0
1 2018-08-30 05:32:10    1933        1 2018-08-30 05:33:10   2009.0
2 2018-08-30 09:10:39    2233        2                 NaT      NaN
3 2018-08-30 10:26:59    2233        3                 NaT      NaN
4 2018-08-30 11:56:25    4459        1 2018-08-30 11:57:25   3012.0
5 2018-08-30 12:30:55    4459        1                 NaT      NaN

有没有办法修复
NaT
值,因为我想让它匹配两列,
clickTime
基本上依赖于
dateTime
,因为没有
dateTime
clickTime
就不可能了。它也不会合并重复的值,只会连接唯一的
userId
  dateTime               userId  session   clickTime             clickId

2018-08-30 02:20:19      2233      1       2018-08-30 02:21:09    1987
2018-08-30 02:20:19      2233      1       2018-08-30 02:23:19    1988
2018-08-30 02:20:19      2233      1       2018-08-30 02:21:09    1989
2018-08-30 02:20:19      2233      1       2018-08-30 02:21:09    1990
pd.merge_asof(
    df1, df2,
    left_on='dateTime',
    right_on='clickTime',
    by='userId',
    direction='nearest'
)

             dateTime  userId  session           clickTime  clickId
0 2018-08-30 02:20:19    2233        1 2018-08-30 02:21:09     1987
1 2018-08-30 05:32:10    1933        1 2018-08-30 05:33:10     2009
2 2018-08-30 09:10:39    2233        2 2018-08-30 02:32:09     1990
3 2018-08-30 10:26:59    2233        3 2018-08-30 02:32:09     1990
4 2018-08-30 11:56:25    4459        1 2018-08-30 11:57:25     3012
5 2018-08-30 12:30:55    4459        1 2018-08-30 11:58:55     3013
pd.merge_asof(
    df1, df2,
    left_on='dateTime',
    right_on='clickTime',
    by='userId',
    direction='nearest',
    tolerance=pd.Timedelta(15, unit='m')
)

             dateTime  userId  session           clickTime  clickId
0 2018-08-30 02:20:19    2233        1 2018-08-30 02:21:09   1987.0
1 2018-08-30 05:32:10    1933        1 2018-08-30 05:33:10   2009.0
2 2018-08-30 09:10:39    2233        2                 NaT      NaN
3 2018-08-30 10:26:59    2233        3                 NaT      NaN
4 2018-08-30 11:56:25    4459        1 2018-08-30 11:57:25   3012.0
5 2018-08-30 12:30:55    4459        1                 NaT      NaN