[英]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 ![]() |
login![]() |
logout![]() |
---|---|---|
1 ![]() |
01/11/2020 ![]() |
03/23/2021 ![]() |
1 ![]() |
08/12/2020 ![]() |
now![]() |
1 ![]() |
01/10/2018 ![]() |
now![]() |
1 ![]() |
02/02/2021 ![]() |
02/03/2021 ![]() |
2 ![]() |
04/05/1990 ![]() |
03/22/2021 ![]() |
3 ![]() |
01/25/2010 ![]() |
02/22/2021 ![]() |
2 ![]() |
06/12/2015 ![]() |
now![]() |
4 ![]() |
now![]() |
now![]() |
what i'm getting:我得到了什么:
id ![]() |
login![]() |
logout![]() |
---|---|---|
1 ![]() |
01/10/2018 ![]() |
now![]() |
2 ![]() |
04/05/1990 ![]() |
now![]() |
3 ![]() |
01/25/2010 ![]() |
02/22/2021 ![]() |
4 ![]() |
now![]() |
now![]() |
how i expect the output to be我如何期望 output
id ![]() |
login![]() |
logout![]() |
---|---|---|
1 ![]() |
01/10/2018 ![]() |
03/23/2021 ![]() |
2 ![]() |
04/05/1990 ![]() |
03/22/2021 ![]() |
3 ![]() |
01/25/2010 ![]() |
02/22/2021 ![]() |
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.