且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

按 pandas 数据框分组并在每组中选择最新的

更新时间:2023-02-05 18:11:58

use idxmax in groupby 和 slice df with loc

use idxmax in groupby and slice df with loc

df.loc[df.groupby('id').date.idxmax()]

    id  product       date
2  220     6647 2014-10-16
5  826     3380 2015-05-19
8  901     4555 2014-11-01