drop_duplicates() 在 Python pandas 停止工作

drop_duplicates() stopped working in Python pandas

此代码之前曾在 python 3 中运行以删除重复值,但在整个数据帧中保留第一次出现。回到我的脚本后,这不再删除 pandas 数据帧中的重复项。

df = df.apply(lambda x: x.drop_duplicates(), axis=1)

所以如果我有

a   b  c
0   1  2
3   4  0
0   8  9
10  0  11

我想得到作为输出

a  b  c
0  1  2
3  4  
   8  9
10   11

我不介意空格 return 为 'nan'

我也尝试了以下

df.drop_duplicates(subset = None, keep='first')

df.drop_duplicates(subset = None, keep='first', inplace =True)

欢迎任何建议/替代方案!

您需要 inplace 为真:

df.drop_duplicates(subset=None, keep='first', inplace=True)

附上数据后,我想你可以使用duplicated

newdf=df[~df.stack().duplicated().unstack()]
newdf
Out[131]: 
      a    b     c
0   0.0  1.0   2.0
1   3.0  4.0   NaN
2   NaN  8.0   9.0
3  10.0  NaN  11.0