当前位置: 代码迷 >> python >> 按列中值的最后一次出现过滤 Pandas 数据集
  详细解决方案

按列中值的最后一次出现过滤 Pandas 数据集

热度:72   发布时间:2023-07-16 11:03:03.0

我有一个这样的数据集:

    Fruit    Qty
0   Apple    1
1   Orange   3
2   Pear     7
3   Orange   1
4   Pear     23
5   Lemon    5

我试图获得这个:

    Fruit    Qty
0   Apple    1
1   Orange   1
2   Pear     23
3   Lemon    5

所以我想通过“水果”列中最后一次出现来过滤我的数据集的行。

IIUC drop_duplicates

s=df.drop_duplicates('Fruit',keep='last')


s
    Fruit  Qty
0   Apple    1
3  Orange    1
4    Pear   23
5   Lemon    5
  相关解决方案