python - 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")
但我需要一种在熊猫中做到这一点的方法。
解决方案
df.drop_duplicates(subset='cust_key')
推荐阅读
- python - 如何从python中的数组中删除分号?
- java - java scheduleWithFixedDelay 内存泄漏
- modal-dialog - Vue.js 2:模式对话框 - 方法成功时关闭
- mysql - Mysql查找所有用户的共同值
- javascript - 忽略承诺拒绝的简单方法
- apache-spark - Python Spark:.distinct().count() 和 countDistinct() 之间的区别
- oracle11g - 视图是否继承从表创建的索引?
- sql-server - 如何使用 Invoke-SQLcmd 插入一行并获取该行的 ID?
- javascript - 迭代到javascript中嵌套对象的末尾
- python - Django,使用“|”:表达式树太大(最大深度 1000)