我在一个数据库中有一系列的电子邮件,它们被分成线程,所以下面的数据框将代表一个单独的对话。我试着计算我和被试的平均反应时间。你知道吗
目前,我是通过一个循环,这似乎是非常低效的。是否有任何我不使用的可用功能?我能不能找到一种方法来筛选出唯一的响应?这样我就可以改变时间,然后用这种方法计算差异。你知道吗
示例数据帧。如下图所示,在回复之前,一方可能会发送多封电子邮件。只有第一个应该用来计算响应时间差
Sender DateTime
Me ISO8601
Me ISO8601
Customer ISO8601
Me ISO8601
Customer ISO8601
Customer ISO8601
Me ISO8601
Me ISO8601
Me ISO8601
Customer ISO8601
当前操作的伪代码(太长,无法放入完整的实际代码)
# Track who responded last
last_comment_type = None
# Track response numbers (number of times each party responded)
customer_response_count = 0
my_response_count = 0
# Determine who the first sender is (doesn't always have to be me)
if df.ix[df.index.values.min()]['ResponseType'] == customer:
last_comment_type = 'Customer'
next_response = df.iloc[1:][df['ResponseType'] = 'Me']
my_total_response_time += next_response - row
my_response_count += 1
else:
last_comment_type = 'Me'
next_response = df.iloc[1:][df['ResponseType'] = 'Customer']
customer_total_response_time += next_response - row
customer_response_count += 1
# Loop over rest of rows and figure out who responded to who.
for row in df.itertuples():
if row.ResponseType == 'Customer' and last_comment_type == 'Me':
next_response = df[df.index > row.Index][df['ResponseType'] != 'Customer']
my_total_response_time += next_response - row
my_response_count += 1
if row.ResponseType == 'Me' and last_comment_type == 'Customer':
next_response = df[df.index > row.Index][df['ResponseType'] != 'Me']
customer_total_response_time += next_response - row
customer_response_count += 1
my_response_avg = my_total_response_time/my_response_count
customer_response_avg = customer_total_response_time/customer_response_count
如果我理解正确,熊猫可以做得更简单。如果您只想计算第一条消息之间的差异:
如果要通过
Sender
计算平均响应时间,可以使用groupby()
:注意:我使用sum/count而不是mean,因为this issue与
groupby
和mean()
在将来可能会被修补。我们把它分解一下。首先,我们需要一个带有可用时间的示例数据帧(这是我建议您在这样的问题中提供的):
这将为您提供如下数据帧:
df['block'] = ((df.Sender != df.Sender.shift()).cumsum())
创建一个新列,其中来自相同Sender
的连续行具有相同的编号:由于我们所关心的(在版本1中)是从一个
Sender
发送的第一条消息到下一个发送方发送的第一条消息之间的时间,因此我们可以删除除第一个block
具有重复值的行以外的所有行:它给出了每个特定于
Sender
的块中每个第一条消息的时间:如果我们想要所有发送者之间的平均时间,我们需要做的就是添加
diff()
和mean()
以获得行之间的平均时间:它给出:
如果我们想通过发送者获得平均时间,您需要额外的步骤。我们为响应时间创建一个新的列,然后按发送者分组,然后计算平均值。你知道吗
结果是:
注意:如果要计算一个发件人的最后一封邮件与下一个发件人的第一封邮件之间的差值:
相关问题 更多 >
编程相关推荐