在 Pandas 中选择 ID 相同但值不同的行

编程入门 行业动态 更新时间:2024-10-25 23:36:22
本文介绍了在 Pandas 中选择 ID 相同但值不同的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我知道这在数据库软件中是可行的,但有没有办法在 Python Pandas 中做到这一点?

I know this is possible in database software but is there any way to do it in Python Pandas?

ID1 ID2 Value 1209345 1203 2 1209345 1204 3 <----- 1209345 1205 4 1209345 1203 2 1209345 1204 7 <----- 1209346 1203 1 1209347 1204 5

我有 ID1,与此相对应,我有多个 ID2 映射到一个值.我需要找到 ID1 和 ID2 匹配但值不同的所有条目.

I have ID1 and, corresponding to that, I have multiple ID2s mapped with a value. I need to find all entries where ID1 and ID2 are matching but the values are different.

我当前的代码统计了ID1和ID2的唯一组合数,但没有计算每个组合的唯一Value:

My current code counts the number of unique combinations of ID1 and ID2, but does not account for unique Value for each combination:

print(df.groupby(['ID1', 'ID2']).size()) ID1 ID2 1209345 1203 2 1204 2 1205 1 1209346 1203 1 1209347 1204 1 dtype: int64

注意:这个问题是为@RohitGirdhar 发布的,他删除了他的原始问题.我发布的解决方案不一定是唯一的或最好的;鼓励其他答案.

Note: This question is posted for @RohitGirdhar who deleted his original question. The solution I post is not necessarily the only or best one; other answers are encouraged.

推荐答案

您可以使用 nunique 和 transform:

You can filter with nunique and transform:

df = df[df.groupby(['ID1', 'ID2'])['Value'].transform('nunique') > 1] print (df) ID1 ID2 Value 1 1209345 1204 3 4 1209345 1204 7

更多推荐

在 Pandas 中选择 ID 相同但值不同的行

本文发布于:2023-10-30 18:57:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1543714.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Pandas   ID

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!