Category | Unit | ID | Time | isReq
1 A x1 t1 0
1 A x1 t2 0
1 A x1 t3 0
1 A x1 t4 0
1 B x2 t5 1
1 B x2 t6 0
1 B x2 t7 0 我正在尝试查找每个类别和单位的ID的唯一数量,以及它们的平均持续时间。最终结果应该如下所示:
Category | Unit | ID_count | time:diff
1 A 1 mean_time_to_completion for all ids in category and unit 在给定的Category和Unit下可以有多个ID。我如何继续使用我必须获得的数据才能得到结果?我可以独立地获得按Category和Unit分组的唯一ID,但在一个查询中获取所有字段很困难。
编辑:-我已经设法通过以下方式执行了必要的操作:
df1.groupby(['Category','Unit','ID'])['Time'].agg(['first','last']).diff(axis = 1).iloc[:,-1].reset_index().groupby(['Category','Unit']).agg({'ID' : 'count','last' : pd.Series.mean})我现在正在尝试计算每个ID在与上面相同的查询中作为总isReq计数的百分比的贡献。欢迎提出任何有用的建议。
发布于 2020-02-19 03:31:06
你可以试试这个-
df.groupby(['Category', 'Unit']).agg(ID_count=('ID','nunique'), time_diff=('Time', 'mean'))https://stackoverflow.com/questions/60287941
复制相似问题