I have a dataframe like
| id | group | person | company | time | timestamp |
|---|---|---|---|---|---|
| 345 | 2020-04-01 | user1 | A | 10:04:05 | |
| 346 | 2020-04-01 | user1 | A | 10:14:05 | |
| 347 | 2020-04-01 | user2 | B | 10:24:05 | |
| 348 | 2020-04-01 | user1 | A | 11:04:05 | |
| 349 | 2020-04-01 | user2 | B | 11:06:05 | |
| ... | ... | ... | ... | ... | |
| 1000 | 2020-04-20 | user1 | AA | 11:04:05 | |
| 1034 | 2020-04-20 | user1 | AA | 12:04:05 | |
| 1078 | 2020-04-21 | user2 | BB | 12:34:05 | |
| 1200 | 2020-04-22 | user1 | AA | 12:40:05 |
This is list of messages where user1 is consultant and userN are clients from different companies. I also added group column where I added the date when this message was sent.
I need to calculate the average time between different type of users, i.e.:
in 2020-04-01 **user1** sent the 1st message in 10:04:05 and **user2** answered in 10:24:05, diff 20 min
and in this day user1 sent the 2nd message in 11:04:05 and user2 answered in 11:06:05, diff is 2 min.
Knowing several diff periods I can calculate mean() and if I have only messages from 1 type of user my average would be 'no answered'
My code is here
fin = fin.reset_index() # reset indexes
# here I wanna leave only the first message of each type of users, convert [user1, user1, user2] to [user1, user2]
test = fin.loc[fin['sender_full_name'].shift() != fin['sender_full_name']]
g = test.groupby('group') # got the series of group
for i in g.groups: # iterate over every group element
id = g.get_group(i).index # got the index of this group
f = test.loc[id] # new dataframe by index
ds = pd.Series(f['timestamp']).reset_index(drop=True) # got all timestamps by date
avg_idx = pd.Series(f['id'])
s1 = pd.Series([])
s2 = pd.Series([])
for j in range(ds.size):
s1 = s1.append([pd.Series(ds[j])], ignore_index=True) if j % 2 == 0 else s1
s2 = s2.append([pd.Series(ds[j])], ignore_index=True) if j % 2 != 0 else s2
s3 = s2.subtract(s1) if len(s2) > 0 else 'без ответа'
s3 = s3.loc[~s3.isna()].mean() if len(s2) > 0 else s3
fin.loc[fin['id'].isin(avg_idx), 'avg'] = s3 # write new value of average
fin
But I got not expected values, also after that I want to drop other rows in group instead of the 1st by, i.e.
from
| id | group | person | company | timestamp |
|---|---|---|---|---|
| 1000 | 2020-04-20 | user1 | AA | 11:04:05 |
| 1034 | 2020-04-20 | user1 | AA | 12:04:05 |
| 1078 | 2020-04-21 | user2 | BB | 12:34:05 |
| 1200 | 2020-04-22 | user1 | AA | 12:40:05 |
to
| id | group | person | company | timestamp |
|---|---|---|---|---|
| 1000 | 2020-04-20 | user1 | AA | 11:04:05 |
| 1078 | 2020-04-21 | user2 | BB | 12:34:05 |
| 1200 | 2020-04-22 | user1 | AA | 12:40:05 |