2 回答
TA贡献1872条经验 获得超4个赞
我认为您的代码按预期工作?
df['TIME_IN'] = pd.to_datetime(df['TIME_IN'])
df['TIME_OUT'] = pd.to_datetime(df['TIME_OUT'])
df['DATE'] = (df['TIME_IN'] - datetime.timedelta(hours=5)).dt.date
df["DURATION"] = df["TIME_OUT"] - df["TIME_IN"]
df.groupby("DATE")['DURATION'].sum()
输入到 groupby
TIME_IN TIME_OUT DATE DURATION
0 2019-05-06 11:46:51 2019-05-06 11:50:36 2019-05-06 00:03:45
1 2019-05-02 20:47:54 2019-05-02 20:52:22 2019-05-02 00:04:28
2 2019-05-05 07:39:02 2019-05-05 07:46:34 2019-05-05 00:07:32
3 2019-05-04 17:28:52 2019-05-04 17:32:57 2019-05-04 00:04:05
4 2019-05-05 14:08:26 2019-05-05 14:14:30 2019-05-05 00:06:04
分组后输出
DATE
2019-05-02 00:04:28
2019-05-04 00:04:05
2019-05-05 00:13:36
2019-05-06 00:03:45
似乎按预期工作。
TA贡献1863条经验 获得超2个赞
您可以使用agg分组对象的功能来总结持续时间,如下所示
import pandas as pd
import numpy as np
np.random.seed(10)
## Generate dummy data for testing
dt_range = pd.date_range("oct-12-2019", "oct-14-2019", freq="H")
arr = []
while len(arr)<10:
i,j = np.random.choice(len(dt_range), 2)
g = np.random.choice(4)
if j>i:
arr.append([g, dt_range[i], dt_range[j]])
df = pd.DataFrame(arr, columns=["group", "time_in", "time_out"])
## Solution
df["duration"] = df["time_out"] - df["time_in"]
df.groupby(df["time_in"].dt.date).agg({"duration":np.sum})
添加回答
举报
