【发布时间】:2021-07-13 08:51:00
【问题描述】:
我有一个像这样的 pandas 数据框,其中我可以有相同的 long 和 lat 组合的行:
初始df:
lon lat name value protection a b c score
0 20 10 canada 563 NaN cat dog elephant 20.0
1 30 10 canada 65 NaN lion tiger cat 30.0
2 40 20 canada 893 NaN dog NaN NaN 20.0
3 40 20 usa 4 NaN horse horse lion 40.0
4 45 15 usa 8593 NaN NaN lion cat 10.0
5 20 10 protection1 100 medium NaN NaN NaN NaN
6 40 20 protection1 20 high NaN NaN NaN NaN
7 50 30 protection1 500 low NaN NaN NaN NaN
但我想要的是:
想要的输出:
lon lat protection a b c score
0 20 10 medium cat dog elephant 20.0
1 30 10 NaN lion tiger cat 30.0
2 40 20 high horse horse lion 40.0
3 45 15 NaN NaN lion cat 10.0
4 50 30 low NaN NaN NaN NaN
输出数据框应包含具有 long 和 lat 列的唯一组合的行,其中仅保留具有最高 score 的行,但如果 long 和 lat 有重复项并且在protection 列这些应该合二为一
【问题讨论】:
-
试试
df.drop_duplicates(subset=['lon'],keep='last') -
我试过
df.drop_duplicates(subset=['lon', 'lat'],keep='last'),但它去掉了太多行 -
请将您的数据框作为文本发布,以便我们重现您的数据框或在您的问题中添加
df.head(10).to_dict()的输出 -
我用 dfs 作为文本编辑了我的帖子
标签: pandas duplicates rows