【问题标题】:Is there an easy way to eliminate duplicate rows in a DataFrame in Python- pandas? [duplicate]有没有一种简单的方法可以在 Python-pandas 中消除 DataFrame 中的重复行? [复制]
【发布时间】:2017-11-14 10:15:45
【问题描述】:

我的问题是我的数据不能很好地代表实际情况,因为它有很多重复的行。考虑以下-

    a    b
1  23   42
2  23   42
3  23   42
4  14   12
5  14   12

我只想要 1 行并消除所有重复项。完成后应该如下所示。

    a    b
1  23   42
2  14   12

有这样的功能吗?

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    让我们使用drop_duplicateskeep='first'

    df2.drop_duplicates(keep='first')
    

    输出:

        a   b
    1  23  42
    4  14  12
    

    【讨论】:

    • 很好的答案,我相信它有效......但是ValueError: Buffer has wrong number of dimensions (expected 1, got 2)
    • 你可以打印你的实际数据的头部吗?
    • 我不能。这是一个安全风险。
    • 多索引数据?看看您是否可以重新创建重现此错误的虚拟数据。
    • @NickTheInventor df2.drop_duplicates(subset=['a','b']) 如果您不想考虑所有列,可以尝试这样的操作。
    猜你喜欢
    • 2015-08-27
    • 1970-01-01
    • 2012-01-05
    • 1970-01-01
    • 1970-01-01
    • 2011-01-03
    • 2012-02-08
    • 2021-05-28
    • 1970-01-01
    相关资源
    最近更新 更多