I am trying to analyze a few things in the following data set using a modified version of code that user @Garret provided, though, I am running into a few issues.
The dataset has a column that shows whether the customer was engaged by a live agent, or an automated machine. I am trying to get the difference between concurrent calls in which the member was first connected to an agent, and then was not. The call must have the same call reason, and it must be placed after the initial call in regards to the timestamp. Also, it is okay for there to be calls for other reasons in between.
Here is the dataset:
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
The output that I am expecting is as follows:
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
So basically, just looking for the difference between two calls in regards to call_reason and connection. The first call being when the member is connected to an agent, the second call has to come after the first based off of the timestamp, has to be for the same reason, and has to be connected to the automated system. It is okay if there are calls placed for other reasons in between. The code I have tried is as follows:
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
The output that I get is:
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
again, the expected output would be:
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
Note: I would love if the solution could be flexible so that I can analyze a few different scenarios such as:
- the difference between calls that are only automated
- the difference between calls that are only connected to agents
- difference between calls when initial call is connected to an agent, and in the second call it doesnt matter which connection type
Additional help with this would be much appreciated!