【问题标题】:Why drop_duplicates function in pandas dose not work with object type?为什么 pandas 中的 drop_duplicates 函数不适用于对象类型?
【发布时间】:2020-08-18 14:37:18
【问题描述】:

我在 pandas python 中使用 drop duplicates 但它对我不起作用!

我使用 in-place=true 但我有对象 d-type 和 date 我该如何解决它,我需要检查所有列 例如

  A      |       B      |    C 

0 |112 | 2003 年 11 月 12 日 |作为

1 |113 | 2003 年 11 月 12 日 |作为

2 |112 | 2003 年 11 月 12 日 |作为

0 和 2 重复,但在我的情况下不显示为重复

【问题讨论】:

标签: python pandas python-2.7 dataframe


【解决方案1】:

您需要在 1 列上使用 drop_duplicates,然后使用该列上的索引来获得结果:

import pandas as pd
data = [[112, '11/12/2003', 'As'],
[113, '11/12/2003', 'As'],
[112, '11/12/2003', 'As']]
df = pd.DataFrame(data, columns=['A', 'B', 'C'])
df[df.index.isin(df['A'].drop_duplicates().index)]

输出:

     A           B   C
0  112  11/12/2003  As
1  113  11/12/2003  As

【讨论】:

  • 谢谢,但在我的情况下,这不起作用,因为我希望不仅检查日期所有列
  • 那么只需调用df.drop_duplicates() 就足够了,不是吗?
猜你喜欢
  • 1970-01-01
  • 2017-04-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-30
  • 1970-01-01
  • 2017-10-06
  • 1970-01-01
  • 2012-01-22
相关资源
最近更新 更多