【问题标题】:Finding Duplicated value acorss groups in Pandas GroupBy在 Pandas GroupBy 中跨组查找重复值
【发布时间】:2020-05-05 12:47:47
【问题描述】:
df = pd.DataFrame({'A': ['foo', 'bar', 'foo', 'bar',
                         'foo', 'bar', 'foo', 'foo'],
                   'B': ['one', 'one', 'two', 'three',
                         'two', 'two', 'one', 'three'],
                   'C': [3,4,5,8,10,12,14,12]})
df.groupby(['A','B']).sum()

如何查找 C 列中的值是否在其他组中也重复? (这里两组都重复了12个)

【问题讨论】:

  • 您好,欢迎来到 Stack Overflow。为了让社区更容易回答您的问题,请添加一个(最小)示例数据集 - 作为数据(或者更好的是,作为生成它的代码)而不是作为图像。要了解更多信息,请访问stackoverflow.com/questions/20109391/…。我很乐意删除我的反对票并在您这样做时尝试回答,所以当您这样做时,请在 cmets 中标记我。
  • @ItamarMushkin 更新了数据集

标签: python python-3.x pandas pandas-groupby


【解决方案1】:

想法是将MultIndex 转换为3 列DataFrame,然后DataFrame.pivot 通过DataFrame.dropna 删除不重复的行,并且常用值在索引中:

df = pd.DataFrame({'A': ['foo', 'bar', 'foo', 'bar',
                         'foo', 'bar', 'foo', 'foo'],
                   'B': ['one', 'one', 'two', 'three',
                         'two', 'two', 'one', 'three'],
                   'C': [3,4,5,8,10,12,14,12]})
df = df.groupby(['A','B']).sum()

common = df.reset_index().pivot('C','A','B').dropna().index
print (common)
Int64Index([12], dtype='int64', name='C')

如果要过滤原始数据,请使用boolean indexing

df = df[df['C'].isin(common)]
print (df)
            C
A   B        
bar two    12
foo three  12

如果希望公共行至少在 2 组中重复,解决方案是:

print (df)  
     A      B   C
0  foo    one   3
1  bar    one   4
2  foo    two   3
3  bar  three   8
4  foo    two  14
5  bar    two  12
6  foo    one  14
7  foo  three  12
8  xxx    yyy   8

df = df.groupby(['A','B']).sum()
print (df)
            C
A   B        
bar one     4
    three   8 <- dupe per bar, three
    two    12 <- dupe per bar, two
foo one    17 <-17 is duplicated per group foo, one, so omited
    three  12 <- dupe per foo, three
    two    17 <-17 is duplicated per group foo, one, so omited
xxx yyy     8 <- dupe per xxx, yyy

common1 = (df.reset_index()
             .pivot_table(index='C',columns='A', values='B', aggfunc='size')
             .notna()
             .sum(axis=1)
            )
common1 = common1.index[common1.gt(1)]
print (common1)
Int64Index([8, 12], dtype='int64', name='C')

df1 = df[df['C'].isin(common1)]
print (df1)
            C
A   B        
bar three   8
    two    12
foo three  12
xxx yyy     8

【讨论】:

    【解决方案2】:

    为了展示一个更有启发性的示例,我在您的源 DataFrame 中添加了一行, 使其包含:

         A      B   C
    0  foo    one   3
    1  bar    one   4
    2  foo    two   5
    3  bar  three   8
    4  foo    two  10
    5  bar    two  12
    6  foo    one  14
    7  foo  three  12
    8  xxx    yyy   8
    

    我将分组结果保存在另一个DataFrame中:

    df2 = df.groupby(['A','B']).sum()
    

    所以它包含:

                C
    A   B        
    bar one     4
        three   8
        two    12
    foo one    17
        three  12
        two    15
    xxx yyy     8
    

    如你所见,C中有两个重复值:128。 请注意,现在 df2 中的索引是 unique

    然后,要显示重复值及其组,运行:

    df2[df2.duplicated(keep=False)].sort_values('C')
    

    得到:

                C
    A   B        
    bar three   8
    xxx yyy     8
    bar two    12
    foo three  12
    

    上面的结果显示了所有重复的值和组(AB) 他们在其中。

    【讨论】:

    • 您的解决方案还解析了每组重复的内容,所以是错误的。检查已编辑的我的答案,您的解决方案还解析 17 行,它解析所有欺骗,而不是每个组的欺骗。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-03
    • 2012-11-24
    • 1970-01-01
    • 2021-05-09
    相关资源
    最近更新 更多