python - pandas drop_duplicates 使用比较函数

标签 python pandas

是否有可能使用 pandas.drop_duplicates使用比较运算符比较特定列中的两个对象以识别重复项?如果不能,还有什么选择?

这是一个可以使用它的例子:

我有一个 pandas DataFrame,它在特定列中有列表作为值,我想根据列 A

删除重复项
import pandas as pd

df = pd.DataFrame( {'A': [[1,2],[2,3],[1,2]]} )
print df

给我

        A
0  [1, 2]
1  [2, 3]
2  [1, 2]

使用 pandas.drop_duplicates

df.drop_duplicates( 'A' )

给我一​​个TypeError

[...]
TypeError: type object argument after * must be a sequence, not itertools.imap

然而,我想要的结果是

        A
0  [1, 2]
1  [2, 3]

我的比较函数会在这里:

def cmp(x,y):
    return x==y

但原则上它可以是其他东西,例如,

def cmp(x,y):
    return x==y and len(x)>1

如何基于比较函数有效地去除重复项?

更重要的是,如果我有更多的列可以分别使用不同的比较函数进行比较,我该怎么办?

最佳答案

选项 1

df[~pd.DataFrame(df.A.values.tolist()).duplicated()]

enter image description here

选项 2

df[~df.A.apply(pd.Series).duplicated()]

关于python - pandas drop_duplicates 使用比较函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39506438/

相关文章:

Python pandas 将行插入 DF 并将缺失值作为 Nan 的快速方法

python - 使用 JQuery 将请求发送到 Flask

python - 将 pandas 列中的字典转换为字典值

python - 从数据框中的值计算增量

python - 消除 PLY 语法中的这种 Shift-Reduce 冲突

尝试使用 Google Maps API 时出现 Python socket.py gaierror

python - 将 Python 输出打印到 Excel 工作表

python - 将数据框转换为元组列表的字典

python - 将 pandas 数据框聚合为多列的百分位数排名

Python Pandas : create rank columns, 移动原始列最大排名