【问题标题】:Find common values between 3 DataFrames?找到 3 个 DataFrame 之间的共同值?
【发布时间】:2018-03-10 21:37:10
【问题描述】:

我有 3 个数据框:df1、df2 和 df3。

df1 = 'num' 'type' 
       23     a 
       34     b 
       89     a 
       90     c

df2 = 'num' 'type' 
       23     a 
       34     b 
       56     a 
       90     c

df3 = 'num' 'type' 
       56     a 
       34     s 
       71     a 
       90     c

我想要的是出现在 2 个或更多 dfs 中的所有 'num' 值的输出,并且我想标记出现了 'num' 值的 dfs 有多少。所以我想要这样的东西:

df = 'num' 'type' 'count' 
       23     a       2 
       34     s       3 
       90     c       3 
       56     a       2

我尝试进行内部合并,但这仅考虑出现在所有 3 个 dfs 中的“num”值,忽略出现在 2/3 dfs 中的值。 解决这个问题的最佳方法是什么?

【问题讨论】:

    标签: python pandas dataframe counter


    【解决方案1】:

    瞧我的朋友

    df_full = pd.concat([df1,df2,df3], axis = 0)
    df_agg = df_full.groupby('num').agg({'type': 'count'})
    df_agg = df_agg.loc[df_agg['type'] >= 2]
    

    【讨论】:

      【解决方案2】:

      这是一个collections.Counter 解决方案,其复杂度为 O(n)。

      如果需要,可以轻松地将计数结果返回到pandas

      from collections import Counter
      
      c = sum((Counter(df['num']) for df in [df1, df2, df3]), Counter())
      
      c_masked = {k: v for k, v in c.items() if v>=2}
      
      # {23: 2, 34: 3, 90: 3, 56: 2}
      
      df = pd.DataFrame.from_dict(c_masked, orient='index')
      
      #     0
      # 23  2
      # 34  3
      # 90  3
      # 56  2
      

      【讨论】:

        【解决方案3】:

        这是使用 groupby 和 size 获得所需结果的另一种方法

        d1 = {'num': [23,34,89,90], 'type': ['a', 'b', 'a', 'c']}
        d2 = {'num': [23,34,56,90], 'type': ['a', 'b', 'a', 'c']}
        d3 = {'num': [56,34,71,90], 'type': ['a', 's', 'a', 'c']}
        
        df1 = pd.DataFrame(data=d1)
        df2 = pd.DataFrame(data=d2)
        df3 = pd.DataFrame(data=d3)
        
        df10 = pd.concat([df1,df2,df3], axis=0)
        # Using groupby with 'num' and 'type' and then using size to get the count.
        # resent_index(name='count') will name the size column as 'count'
        df20 = df10.groupby(['num','type']).size().reset_index(name='count')
        
        # getting the index with 'count' >= 2 and storing those in df_out.
        df_out = df20[df20['count'] >=2].reset_index(drop=True)
        print(df_out)
        

        输出如下:

           num type  count
        0   23    a      2
        1   34    b      2
        2   56    a      2
        3   90    c      3
        

        供参考

        print(df20)
           num type  count
        0   23    a      2
        1   34    b      2
        2   34    s      1
        3   56    a      2
        4   71    a      1
        5   89    a      1
        6   90    c      3
        

        【讨论】:

        • 你在开玩笑吗,这是我的解决方案
        • @NOOBIE,当您运行代码时,输​​出有 2 列“num”和“type”,这不是要求输出的内容。如果您看到要求,则输出旨在包含 3 列,其中包含“num”、“type”和“count”。我的解决方案使用 .size 并创建与您的解决方案不同的列“计数”。我希望您能看到与您提交的解决方案的不同之处。
        猜你喜欢
        • 2021-01-30
        • 1970-01-01
        • 2013-11-10
        • 2015-10-27
        • 1970-01-01
        • 1970-01-01
        • 2023-03-26
        • 1970-01-01
        • 2018-12-05
        相关资源
        最近更新 更多