在 Python Pandas 中删除多列中的所有重复行

Drop all duplicate rows across multiple columns in Python Pandas(在 Python Pandas 中删除多列中的所有重复行)
本文介绍了在 Python Pandas 中删除多列中的所有重复行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

pandas drop_duplicates 函数非常适合唯一化"数据帧.但是,要传递的关键字参数之一是 take_last=Truetake_last=False,而我想删除在列子集中重复的所有行.这可能吗?

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

例如,我想删除与列 AC 匹配的行,所以这应该删除第 0 行和第 1 行.

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

推荐答案

现在有了 drop_duplicates 和 keep 参数.

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)

这篇关于在 Python Pandas 中删除多列中的所有重复行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

python count duplicate in list(python在列表中计数重复)
drop_duplicates not working in pandas?(drop_duplicates 在 pandas 中不起作用?)
Get unique items from list of lists?(从列表列表中获取唯一项目?)
How to install python package with a different name using PIP(如何使用 PIP 安装具有不同名称的 python 包)
How to quot;select distinctquot; across multiple data frame columns in pandas?(如何“选择不同的?跨越 pandas 中的多个数据框列?)
Intersection of two lists, keeping duplicates in the first list(两个列表的交集,在第一个列表中保留重复项)