【问题标题】:Calculating the days difference between previous visit time to current visit time for same customer (same group) in Pandas计算 Pandas 中同一客户(同一组)上次访问时间与当前访问时间之间的天数差异
【发布时间】:2021-10-04 08:07:38
【问题描述】:

我正在尝试计算客户上次访问时间与客户最近访问时间之间的时间差(以天为单位)。

time difference = latest in time - previous out time

这是输入数据的示例

样本输出表

目前我尝试过的基于客户 ID 和排名的 groupby 方法

temp['RANK'] = temp.groupby('customer ID')['in time'].rank(ascending=True)

但我不确定如何计算差异。

【问题讨论】:

标签: python pandas dataframe pandas-groupby sklearn-pandas


【解决方案1】:

您可以使用GroupBy.shift() 获取组内以前的out time。减去当前的in time。然后,使用dt.days获取组内in timeout time之间的timedelta天数,如下:

# convert date strings to datetime format
df['out time'] = pd.to_datetime(df['out time'], dayfirst=True)
df['in time'] = pd.to_datetime(df['in time'], dayfirst=True)

df['Visit diff (in days)'] = (df['in time'] - df['out time'].groupby(df['customer ID']).shift()).dt.days

数据输入:

print(df)

   customer ID             out time              in time
0            1  05-12-1999 15:20:07  05-12-1999 14:23:31
1            1  21-12-1999 09:59:34  21-12-1999 09:41:09
2            2  05-12-1999 11:53:34  05-12-1999 11:05:37
3            2  08-12-1999 19:55:00  08-12-1999 19:40:10
4            3  01-12-1999 15:15:26  01-12-1999 13:08:11
5            3  16-12-1999 17:10:09  16-12-1999 16:34:10

结果:

print(df)

   customer ID            out time             in time  Visit diff (in days)
0            1 1999-12-05 15:20:07 1999-12-05 14:23:31                   NaN
1            1 1999-12-21 09:59:34 1999-12-21 09:41:09                  15.0
2            2 1999-12-05 11:53:34 1999-12-05 11:05:37                   NaN
3            2 1999-12-08 19:55:00 1999-12-08 19:40:10                   3.0
4            3 1999-12-01 15:15:26 1999-12-01 13:08:11                   NaN
5            3 1999-12-16 17:10:09 1999-12-16 16:34:10                  15.0

【讨论】:

  • 感谢@jezrael,我很高兴我的解决方案受到顶级大师的喜爱:-)
【解决方案2】:

您可以尝试以下方法:

temp.groupby('customer ID').apply(lambda x: (x['in time'].max() - x['out time'].min()).days )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-14
    • 2021-05-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多