python dataframe删除重复行,删除Python Pandas中的所有重复行

The pandas drop_duplicates function is great for "uniquifying" a dataframe. However, one of the keyword arguments to pass is take_last=True or take_last=False, while I would like to drop all rows which are duplicates across a subset of columns. Is this possible?

A B C

0 foo 0 A

1 foo 1 A

2 foo 1 B

3 bar 1 A

As an example, I would like to drop rows which match on columns A and C so this should drop rows 0 and 1.

解决方案

This is much easier in pandas now with drop_duplicates and the keep parameter.

import pandas as pd

df = pd.DataFrame({"A":["foo", "foo", "foo", "bar"], "B":[0,1,1,1], "C":["A","A","B","A"]})

df.drop_duplicates(subset=['A', 'C'], keep=False)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值