【问题标题】:Python Pandas : How to select two equal column per row of a dataframePython Pandas:如何在数据帧的每行中选择两个相等的列
【发布时间】:2015-06-29 21:16:34
【问题描述】:

这是我的数据框“dfm”:

match   org_o                         group 
012       012 Smile Communications     92   
012       012 Smile                    92   
10types   10TYPES                      93   
10types   10types.com                  93   
360works  360WORKS                     94   
360works  360works.com                 94   
400 IBM   AS/400 Division              36   
6c f3f    IBM Internal US Division     36   

我想选择具有相同“组”编号以及相同“匹配”的行。所以结果应该是这样的:

   match    org_o                         group 
    012       012 Smile Communications     92   
    012       012 Smile                    92   
    10types   10TYPES                      93   
    10types   10types.com                  93   
    360works  360WORKS                     94   
    360works  360works.com                 94

有人知道我如何在 python pandas 中做到这一点吗?

【问题讨论】:

    标签: python select indexing pandas group-by


    【解决方案1】:

    在“组”和“匹配”上执行groupby,然后在“org_o”的计数上执行filter > 1:

    In [245]:
    
    df.groupby(['group', 'match']).filter(lambda x: x['org_o'].count() > 1)
    Out[245]:
          match                     org_o  group
    0       012  012 Smile Communications     92
    1       012                 012 Smile     92
    2   10types                   10TYPES     93
    3   10types               10types.com     93
    4  360works                  360WORKS     94
    5  360works              360works.com     94
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-30
      • 2018-12-30
      • 1970-01-01
      • 2018-10-30
      • 2020-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多