【问题标题】:Removing duplictes appearing in two or more columns Python删除出现在两列或多列Python中的重复项
【发布时间】:2020-07-11 17:19:43
【问题描述】:

同样的问题,但没有帮助。 How To Solve KeyError: u"None of [Index([..], dtype='object')] are in the [columns]" 第一次尝试:

df = pd.read_csv('ABCD.csv', index_col=['A'])
df=df.drop_duplicates(['A'],['B'])

KeyError: Index(['Sample_ID'], dtype='object')

在这里我发现无法删除索引本身,所以我从顶部删除了它:

df = pd.read_csv('ABCD.csv')
df=df.drop_duplicates(['A'],['B'],keep = 'first')

TypeError: drop_duplicates() 为参数 'keep' 获得了多个值

当我打印 df(type) 它发布 "DataFrame" 时,可能是什么问题?

【问题讨论】:

  • 去掉]就可以使用inplace=True

标签: python duplicates subset


【解决方案1】:

我以为是

df=df.drop_duplicates(['A', 'B'],keep = 'first')

代替:

df=df.drop_duplicates(['A'],['B'],keep = 'first')

子集必须是列的列表,不能与多个参数分开: 子集列标签或标签序列,可选 doc

PS:你应该使用df.drop_duplicates(['A', 'B'], keep='first', inplace=True),添加inplace时不需要分配回df

【讨论】:

  • 谢谢,太尴尬了,我现在也改用“inplace Ttrue”了。
猜你喜欢
  • 2022-01-17
  • 2023-03-20
  • 2023-03-20
  • 1970-01-01
  • 2011-10-04
  • 1970-01-01
  • 2021-02-09
  • 2022-12-15
  • 2017-03-02
相关资源
最近更新 更多