且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Python Pandas删除重复的单元格-保留行

更新时间:2023-01-21 19:01:20

使用简单的 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