我和我的朋友开始了一个小项目,我们跟踪我们的Discord语音聊天活动。提取的数据如下所示:
<表类>
用户
操作
datetime
tbody><<tr>userA # 1234 与 2021-09-07 15:05:35 userB # 5678 与 2021-09-07 15:06:21 userA # 1234 离开 2021-09-07 17:04:40 userB # 5678 离开 2021-09-07 17:04:41 表类>
我将转向并找出差异
import pandas as pd
df = pd.read_csv('path/to/file.csv')
df['datetime'] = pd.to_datetime(df['datetime'])
df_p = df.pivot('user', 'action', 'datetime')
df_p['duration'] = df_p.diff(axis=1)['leave']
df_p.to_csv('path/to/new/file.csv')
action join leave duration
user
userA#1234 2021-09-07 15:05:35 2021-09-07 17:04:40 0 days 01:59:05
userB#5678 2021-09-07 15:06:21 2021-09-07 17:04:41 0 days 01:58:20