简体   繁体   English

Python 按两列分组,然后得到最早和最晚日期

[英]Python Group by two columns and then get the earliest and latest date

When I was trying to get the earliest and latest date after groupby, I found that max results will be attached after min:当我试图获取 groupby 之后的最早和最晚日期时,我发现 max 结果将在 min 之后附加:

ATR_table.groupby(['USAGEID', 'STAT']).agg({'DATADTTM':'min','DATADTTM':'max'})
USAGEID使用 ID STAT统计数据 DATADTTM数据DTTM
10140 10140 0 0 2020-01-01 2020-01-01
10140 10140 1 1 2020-01-01 2020-01-01
10141 10141 0 0 2020-01-01 2020-01-01
10141 10141 1 1 2020-01-01 2020-01-01
10140 10140 0 0 2020-07-18 2020-07-18
10140 10140 1 1 2020-07-18 2020-07-18
10141 10141 0 0 2020-07-18 2020-07-18
10141 10141 1 1 2020-07-18 2020-07-18

Is there a way that I can have the following result by using groupby?有没有办法通过使用 groupby 来获得以下结果?

USAGEID使用 ID STAT统计数据 DATADTTM Min DATADTTM 最小值 DATADTTM Max DATADTTM 最大值
10140 10140 0 0 2020-01-01 2020-01-01 2020-07-18 2020-07-18
10140 10140 1 1 2020-01-01 2020-01-01 2020-07-18 2020-07-18
10141 10141 0 0 2020-01-01 2020-01-01 2020-07-18 2020-07-18
10141 10141 1 1 2020-01-01 2020-01-01 2020-07-18 2020-07-18

If you have no other columns, you could simply pass a plain list:如果你没有其他列,你可以简单地传递一个简单的列表:

ATR_table.groupby(['USAGEID', 'STAT']).agg(['min', 'max'])

If you want to be able to use other functions on other columns, you should include a list in the dictionary:如果您希望能够在其他列上使用其他功能,您应该在字典中包含一个列表:

ATR_table.groupby(['USAGEID', 'STAT']).agg({'DATADTTM':['min', 'max']})

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

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