首页 > 解决方案 > Python pandas基于一列去重数据框

问题描述

我有一个dfCust像这样的数据框():

|cust_key|first_name|last_name|address        |
-----------------------------------------------
|12345   |John      |Doe      |123 Some street|
|12345   |John      |Doe      |123 Some st    |
|67890   |Jane      |Doe      |456 Some street|

我想基本上删除重复的记录,使该cust_key字段是唯一的。我不关心被删除的记录,在发生这种情况时,地址已经被重复数据删除,所以唯一漏掉的是拼写错误。我想要以下结果数据框:

|cust_key|first_name|last_name|address        |
-----------------------------------------------
|12345   |John      |Doe      |123 Some street|
|67890   |Jane      |Doe      |456 Some street|

在 R 中,这基本上是这样完成的:

dfCust <- unique(setDT(dfCust), by = "cust_key")

但我需要一种在熊猫中做到这一点的方法。

标签: pythonpandas

解决方案


df.drop_duplicates(subset='cust_key')

推荐阅读