Skip to main content
 首页 » 编程设计

python之从 python 中的 groupby 对象中选择特定行

2025年05月04日15tuyile006

id    marks  year  
1     18      2013 
1     25      2012 
3     16      2014 
2     16      2013 
1     19      2013 
3     25      2013 
2     18      2014 

假设现在我通过 python 命令对上面的 id 进行分组。
分组 = file.groupby(file.id)

我想得到一个新文件,每个组中只有最近一年的行是该组全年中最高的。

请告诉我命令,我正在尝试使用 apply 但它只会给出 bool 表达式。我想要最新年份的整行。

请您参考如下方法:

我用这个拼凑了这个:Python : Getting the Row which has the max value in groups using groupby

所以基本上我们可以按 'id' 列分组,然后在 'year' 列上调用 transform 并创建一个 bool 索引,其中年份与每个 'id' 的最大年份值相匹配:

In [103]: 
 
df[df.groupby(['id'])['year'].transform(max) == df['year']] 
Out[103]: 
   id  marks  year 
0   1     18  2013 
2   3     16  2014 
4   1     19  2013 
6   2     18  2014