计算文本字段交互之间的时间



我有一个文本字段交互的数据集,这些交互在我的应用程序的几十个用户中持续了几个月。我正在尝试计算熊猫中按键之间的平均时间。数据看起来像这样:

timestamp                before_text     after_text
1453481138188                  NULL               a
1453481138600                     a              ab 
1453481138900                    ab             abc
1453481139400                   abc            abcd
1453484000000    Enter some numbers               1
1453484000100                     1              12
1453484000600                    12             123

timestamp包含用户按键的unix时间,before_text是用户按键前的文本字段,after_text是按键后的字段。

做这件事最好的方法是什么?我知道这并不像做这样简单

(df["timestamp"] - df["timestamp"].shift()).mean()

因为这将在两个相互作用之间的边界上计算出非常大的时间差。似乎最好的方法是将每行的一些函数传递给df.groupby,这样我就可以将上面的代码片段应用于每行。如果我有这个magic_function,我可以做一些类似的事情:

df.groupby(magic_function).apply(lambda x: x["timestamp"] - x["timestamp"].shift()).mean()

实现magic_function的好方法是什么?还是我认为这一切都错了?

我会通过计算"before"one_answers"after"之间的文本差异来实现。如果差异大于某个阈值,则这是一个新会话。

它需要from Levenshtein import distance as ld。我通过pip安装了它,如下所示:

pip install python-levenshtein

然后:

from Levenshtein import distance as ld
import pandas as pd
# taking just these two columns and transposing and back filling.
# I back fill for one reason, to fill that pesky NA with after text.
before_after = df[['before_text', 'after_text']].T.bfill()
distances = before_after.apply(lambda x: ld(*x))
# threshold should be how much distance constitutes an obvious break in sessions.
threshold = 2
magic_function = (distances > 2).cumsum()
df.groupby(magic_function) 
  .apply(lambda x: x["timestamp"] - x["timestamp"].shift()) 
  .mean()
362.4

您的问题本质上是确定一个给定的交互何时停止,另一个交互何时开始。也许可以计算timestamp s之间的差值,如果大于阈值,则设置一个可以分组的标志。

thresh = 1e5
ts = (df['timestamp'] - df['timestamp'].shift()) > thresh
grp = [0]
for i in range(len(ts)):
    if ts.iloc[i]:
        grp.append(grp[-1] + 1)
    else:
        grp.append(grp[-1])
grp.append(grp[-1])
df['grouper'] = grp

现在,您可以简单地按如下方式分组:grouped = df.groupby('grouper'),然后减去组中的timestamp,计算平均差。

我正在想办法避免循环,但在那之前试着这样做,让我知道它是怎么回事。

最新更新