我知道这在数据库软件中是可行的,但有没有办法在 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 相同但值不同的行
发布评论