我正在尝试使用用户@Garret提供的修改后的代码版本来分析以下数据集中的一些内容,但是我遇到了一些问题。
数据集的一列显示客户是被现场代理商还是自动机雇用。我正在尝试获取并发调用之间的区别,在该并发调用中,成员首先连接到代理,然后没有连接。该呼叫必须具有相同的呼叫原因,并且就时间戳而言,它必须放置在初始呼叫之后。此外,由于其他原因,在两者之间也可以打电话。
这是数据集:
data = [['bob13', 1, 'returns','automated',' 2019-08-18 10:12:00'],['bob13', 0, 'returns','automated',' 2019-03-18 10:12:00'],\
['bob13', 8, 'returns','agent',' 2019-04-18 10:15:00'],['rach2', 2, 'shipping','automated',' 2019-04-19 10:15:00'],\
['bob13', 0, 'returns','agent',' 2019-05-18 11:12:00'],['rach2', 0, 'shipping','agent',' 2019-04-18 11:15:00'],\
['bob13', 3, 'returns','agent',' 2019-02-18 10:12:00'],['rach2', 8, 'shipping','agent',' 2019-05-19 10:15:00'],\
['rach2', 7, 'shipping','automated',' 2019-06-19 10:15:00'],['roy', 4, 'exchange','agent','2019-03-26 17:36:00'],\
['roy', 5, 'exchange','automated','2019-01-28 09:48:00']]
df = pd.DataFrame(data, columns = ['member_id', 'survey_score','call_reason','connection','time_stamp'])
df.sort_values(by=['time_stamp']).head(20)
member_id survey_score call_reason connection time_stamp
6 bob13 3 returns agent 2019-02-18 10:12:00
1 bob13 0 returns automated 2019-03-18 10:12:00
2 bob13 8 returns agent 2019-04-18 10:15:00
5 rach2 0 shipping agent 2019-04-18 11:15:00
3 rach2 2 shipping automated 2019-04-19 10:15:00
4 bob13 0 returns agent 2019-05-18 11:12:00
7 rach2 8 shipping agent 2019-05-19 10:15:00
8 rach2 7 shipping automated 2019-06-19 10:15:00
0 bob13 1 returns automated 2019-08-18 10:12:00
10 roy 5 exchange automated 2019-01-28 09:48:00
9 roy 4 exchange agent 2019-03-26 17:36:00
我期望的输出如下:
member_id call_reason automated agent score differential
bob13 returns 0 3 -3
bob13 returns 1 0 1
rach2 shipping 2 0 2
rach2 shipping 7 8 -1
因此,基本上,只需查找关于call_reason和connection的两个调用之间的区别。第一个呼叫是在成员连接到座席时,第二个呼叫必须基于时间戳而在第一个呼叫之后发出,出于相同的原因,并且必须连接到自动化系统。如果之间由于其他原因拨打了电话也可以。我尝试过的代码如下:
grp = df.query('connection=="automated"').\
groupby(['member_id', 'call_reason'])
df['OutId'] = grp.time_stamp.transform(lambda x: x.rank())
df.head(10)
grp = df.groupby(['member_id', 'call_reason'])
df['Id'] = grp.OutId.transform(lambda x: x.bfill())
df.head(10)
agent = df.query('connection=="agent"').\
groupby(['member_id', 'call_reason', 'Id']).survey_score.last()
automated = df.query('connection=="automated"').\
groupby(['member_id', 'call_reason', 'Id']).survey_score.last()
ddf = pd.concat([automated, agent], axis=1,
keys=['automated', 'agent'])
ddf['score_differential'] = ddf.automated - ddf.agent
我得到的输出是:
ddf.dropna().head(10)
automated agent score_differential
member_id call_reason Id
rach2 shipping 2.0 7 8.0 -1.0
roy exchange 1.0 5 4.0 1.0
再次,预期输出将是:
member_id call_reason automated agent score differential
bob13 returns 0 3 -3
bob13 returns 1 0 1
rach2 shipping 2 0 2
rach2 shipping 7 8 -1
注意:如果解决方案可以灵活一些,我会很乐意,这样我就可以分析一些不同的情况,例如:
我们将不胜感激!
答案 0 :(得分:2)
您可以通过创建一个函数,然后将该函数应用于groupby中的组来实现此目的。
设置初始数据框:
import pandas as pd
data = [['bob13', 1, 'returns','automated',' 2019-08-18 10:12:00'],['bob13', 0, 'returns','automated',' 2019-03-18 10:12:00'],\
['bob13', 8, 'returns','agent',' 2019-04-18 10:15:00'],['rach2', 2, 'shipping','automated',' 2019-04-19 10:15:00'],\
['bob13', 0, 'returns','agent',' 2019-05-18 11:12:00'],['rach2', 0, 'shipping','agent',' 2019-04-18 11:15:00'],\
['bob13', 3, 'returns','agent',' 2019-02-18 10:12:00'],['rach2', 8, 'shipping','agent',' 2019-05-19 10:15:00'],\
['rach2', 7, 'shipping','automated',' 2019-06-19 10:15:00'],['roy', 4, 'exchange','agent','2019-03-26 17:36:00'],\
['roy', 5, 'exchange','automated','2019-01-28 09:48:00']]
df = pd.DataFrame(data, columns = ['member_id', 'survey_score','call_reason','connection','time_stamp'])
df.sort_values(by=['time_stamp']).head(20)
df['time_stamp'] = pd.to_datetime(df['time_stamp'])
df
member_id survey_score call_reason connection time_stamp
0 bob13 1 returns automated 2019-08-18 10:12:00
1 bob13 0 returns automated 2019-03-18 10:12:00
2 bob13 8 returns agent 2019-04-18 10:15:00
3 rach2 2 shipping automated 2019-04-19 10:15:00
4 bob13 0 returns agent 2019-05-18 11:12:00
5 rach2 0 shipping agent 2019-04-18 11:15:00
6 bob13 3 returns agent 2019-02-18 10:12:00
7 rach2 8 shipping agent 2019-05-19 10:15:00
8 rach2 7 shipping automated 2019-06-19 10:15:00
9 roy 4 exchange agent 2019-03-26 17:36:00
10 roy 5 exchange automated 2019-01-28 09:48:00
每当我尝试解决这样的问题时,我都会组成一个特定的小组。因此,我只是隔离了bob13,并尝试复制以达到我们想要的bob。导致我执行了一系列特定的步骤,然后将其放入函数中:
我们按时间对数据框进行排序,然后创建名为next_connection和'next_score'的新列。这些将结果从下一个结果中移出,以便我们将其放在该行中。我们删除所有丢失的内容(因为没有下一个,所以是该组的最后一个),我们隔离连接为agent
且next_connection为automated
的任何行。我们将列重命名以匹配您的输出,然后计算得分差异。
def function_(df):
df = df.sort_values('time_stamp')
df['next_connection'] = df.connection.shift(-1)
df['next_score'] = df.survey_score.shift(-1)
df = df.dropna()
df = df[(df.connection == 'agent') & (df.next_connection == 'automated')]
df = df.rename(columns={'survey_score':'agent', 'next_score':'automated'})
df['score differential'] = df['automated'] - df['agent']
return df
现在,我们将其应用于按member_id
和call_reason
分组的数据框。
g = df.groupby(['member_id', 'call_reason']).apply(function_)
g[['member_id','call_reason','automated','agent','score differential']].reset_index(drop=True)
member_id call_reason automated agent score differential
0 bob13 returns 0.0 3 -3.0
1 bob13 returns 1.0 0 1.0
2 rach2 shipping 2.0 0 2.0
3 rach2 shipping 7.0 8 -1.0