Python Pandas删除重复的单元格-保留行 [英] Python Pandas Remove Duplicate Cells - Keep the rows
本文介绍了Python Pandas删除重复的单元格-保留行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图在保留其余行的同时,根据单个列删除特定列的重复值.
I am trying to remove duplicates values of specific columns based on a single column, while keeping the rest of the row.
df = pd.DataFrame({'A':[1,2,3,4],'B':[5,5,6,7],'C':['a','a','b',c'], D:['c','d','e','f']})
我想删除A列中的值;B基于C列中的重复项,但保留了D列的所有内容.
I want to delete the values in column A & B based off the duplicates in column C, but keeping all of column D.
预期输出:
A B C D
1 5 a c
d
3 6 b e
4 7 c f
推荐答案
使用简单的 loc
df.loc[df.C.duplicated(), ['A', 'B']] = ''
A B C D
0 1 5 a c
1 a d
2 3 6 b e
3 4 7 c f
还可以使用 np.nan
代替空字符串,以免与dtypes混淆
Can also use np.nan
instead of empty string not to mess with the dtypes
这篇关于Python Pandas删除重复的单元格-保留行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文