Reputation: 123
When I was trying to get the earliest and latest date after groupby, I found that max results will be attached after min:
ATR_table.groupby(['USAGEID', 'STAT']).agg({'DATADTTM':'min','DATADTTM':'max'})
USAGEID | STAT | DATADTTM |
---|---|---|
10140 | 0 | 2020-01-01 |
10140 | 1 | 2020-01-01 |
10141 | 0 | 2020-01-01 |
10141 | 1 | 2020-01-01 |
10140 | 0 | 2020-07-18 |
10140 | 1 | 2020-07-18 |
10141 | 0 | 2020-07-18 |
10141 | 1 | 2020-07-18 |
Is there a way that I can have the following result by using groupby?
USAGEID | STAT | DATADTTM Min | DATADTTM Max |
---|---|---|---|
10140 | 0 | 2020-01-01 | 2020-07-18 |
10140 | 1 | 2020-01-01 | 2020-07-18 |
10141 | 0 | 2020-01-01 | 2020-07-18 |
10141 | 1 | 2020-01-01 | 2020-07-18 |
Upvotes: 0
Views: 303
Reputation: 148965
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']})
Upvotes: 1