按多列分组以查找重复的行 pandas [英] Grouping by multiple columns to find duplicate rows pandas
本文介绍了按多列分组以查找重复的行 pandas 的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 df
id val1 val2
1 1.1 2.2
1 1.1 2.2
2 2.1 5.5
3 8.8 6.2
4 1.1 2.2
5 8.8 6.2
我想按 val1 和 val2
进行分组,并仅获取具有相同 val1 和 val2
组合多次出现的行的相似数据帧.
I want to group by val1 and val2
and get similar dataframe only with rows which has multiple occurance of same val1 and val2
combination.
最终df
:
id val1 val2
1 1.1 2.2
4 1.1 2.2
3 8.8 6.2
5 8.8 6.2
推荐答案
您需要 duplicated
与参数 subset
用于指定列以 keep=False
检查所有重复的掩码和按布尔索引
过滤:
You need duplicated
with parameter subset
for specify columns for check with keep=False
for all duplicates for mask and filter by boolean indexing
:
df = df[df.duplicated(subset=['val1','val2'], keep=False)]
print (df)
id val1 val2
0 1 1.1 2.2
1 1 1.1 2.2
3 3 8.8 6.2
4 4 1.1 2.2
5 5 8.8 6.2
详情:
print (df.duplicated(subset=['val1','val2'], keep=False))
0 True
1 True
2 False
3 True
4 True
5 True
dtype: bool
这篇关于按多列分组以查找重复的行 pandas 的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文