简体   繁体   English

根据最近和最近的日期对公共 ID 进行分组

[英]Group common ID's based on most and the least recent date

Trying to take the highest and the lowest date from two fields from my data and group them based on the ids.尝试从我的数据中的两个字段中获取最高和最低日期,并根据 id 对它们进行分组。 I noticed that my date fields got a string which is blocking the sort and restricting me from getting the right results.我注意到我的日期字段有一个字符串,它阻止排序并限制我获得正确的结果。

my data set --df我的数据集--df

id ID login登录 logout登出
1 1 01/11/2020 2020 年 1 月 11 日 03/23/2021 2021 年 3 月 23 日
1 1 08/12/2020 2020 年 8 月 12 日 now现在
1 1 01/10/2018 2018 年 1 月 10 日 now现在
1 1 02/02/2021 2021 年 2 月 2 日 02/03/2021 2021 年 2 月 3 日
2 2 04/05/1990 1990 年 4 月 5 日 03/22/2021 2021 年 3 月 22 日
3 3 01/25/2010 2010 年 1 月 25 日 02/22/2021 2021 年 2 月 22 日
2 2 06/12/2015 2015 年 6 月 12 日 now现在
4 4 now现在 now现在

what i'm getting:我得到了什么:

id ID login登录 logout登出
1 1 01/10/2018 2018 年 1 月 10 日 now现在
2 2 04/05/1990 1990 年 4 月 5 日 now现在
3 3 01/25/2010 2010 年 1 月 25 日 02/22/2021 2021 年 2 月 22 日
4 4 now现在 now现在

how i expect the output to be我如何期望 output

id ID login登录 logout登出
1 1 01/10/2018 2018 年 1 月 10 日 03/23/2021 2021 年 3 月 23 日
2 2 04/05/1990 1990 年 4 月 5 日 03/22/2021 2021 年 3 月 22 日
3 3 01/25/2010 2010 年 1 月 25 日 02/22/2021 2021 年 2 月 22 日
4 4 now现在 now现在
my code:
sample= {'login':'min', 'logout':'max'}
final= df.groupby(['id'], sort=True).agg(sample)

Is anything wrong with my approach or a better way in python to solve this problem?我的方法或 python 中解决此问题的更好方法有什么问题吗? or Are there other smart ways to avoid strings other than replacing the strings from the df?或者除了替换df中的字符串之外,还有其他避免字符串的聪明方法吗? (I hail from sql,so still getting used to pythonic stuffs:) ) thx in advance (我来自 sql,所以仍然习惯于 pythonic 的东西:) 提前谢谢

That's because 'now' > '03/23/2021' as far as string comparison goes.那是因为就字符串比较而言, 'now' > '03/23/2021' You can try replace now with a smaller string:now可以尝试用较小的字符串替换:

tmp_now = '000000'
(df.replace('now',tmp_now)
   .groupby(['id'], sort=True).agg(sample)
   .replace(tmp_now,'now')
)

Output: Output:

         login      logout
id                        
1   01/10/2018  03/23/2021
2   04/05/1990  03/22/2021
3   01/25/2010  02/22/2021
4          now         now

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM