删除重复的 pandas df [英] Remove duplicates of pandas df
本文介绍了删除重复的 pandas df的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
尝试使用 DataFrame.drop_duplicates 参数但没有成功,因为重复项没有被删除.
Trying use the DataFrame.drop_duplicates parameters but without luck as the duplicates are not being removed.
希望根据inc_id"列删除.如果在该列中找到重复项,则应仅保留最后一行.
Looking to remove based on column "inc_id". If find duplicates in that column should keep only the last row.
我的 df 是:
inc_id inc_cr_date
0 1049670 121
1 1049670 55
2 1049667 121
3 1049640 89
4 1049666 12
5 1049666 25
输出应该是:
inc_id inc_cr_date
0 1049670 55
1 1049667 121
2 1049640 89
3 1049666 25
代码是:
df = df.drop_duplicates(subset='inc_id', keep="last")
知道我在这里遗漏了什么吗?谢谢.
Any idea what am I missing here? Thanks.
推荐答案
我认为您只是在寻找 删除原始索引:
I think you are just looking to drop the original index:
In [11]: df.drop_duplicates(subset='inc_id', keep="last").reset_index(drop=True)
Out[11]:
inc_id inc_cr_date
0 1049670 55
1 1049667 121
2 1049640 89
3 1049666 25
这篇关于删除重复的 pandas df的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文