【问题标题】:Python Pandas: How to groupby and compare columnsPython Pandas:如何分组和比较列
【发布时间】:2015-04-29 14:06:59
【问题描述】:

这是我的数据农场“df”:

match           name                   group  
adamant         Adamant Home Network   86   
adamant         ADAMANT, Ltd.          86   
adamant bild    TOV Adamant-Bild       86   
360works        360WORKS               94   
360works        360works.com           94

每个组号我想一一比较名称,看看它们是否与“匹配”列中的同一个词匹配。

所以期望的输出将是计数:

 If they match we count it as 'TP' and if not we count it as 'FN'.

我有一个想法,计算每个组数的匹配词数,但这对我想要的完全没有帮助:

df.groupby(group).count() 

有没有人知道怎么做?

【问题讨论】:

  • 你能发布想要的输出吗,谢谢
  • @EdChum 我编辑了这个问题。谢谢
  • 您能否解释一下您认为匹配的内容,您是否只是检查“名称”中是否存在“匹配”中的单词?
  • @EdChum 匹配意味着当我们成对比较来自相同组号的单词时,它们的“匹配”列中有相同的单词

标签: python pandas group-by comparison


【解决方案1】:

如果我能很好地理解你的问题,这应该可以解决问题:

import re
import pandas


df = pandas.DataFrame([['adamant', 'Adamant Home Network', 86], ['adamant', 'ADAMANT, Ltd.', 86],
                       ['adamant bild', "TOV Adamant-Bild", 86], ['360works', '360WORKS', 94],
                       ['360works ', "360works.com ", 94]], columns=['match', 'name', 'group'])


def my_function(group):
    for i, row in group.iterrows():
        if ''.join(re.findall("[a-zA-Z]+", row['match'])).lower() not in ''.join(
                re.findall("[a-zA-Z]+", row['name'])).lower():
            # parsing the names in each columns and looking for an inclusion
            # if one of the inclusion fails, we return 'FN'
            return 'FN'
    # if all inclusions succeed, we return 'TP'
    return 'TP'


res_series = df.groupby('group').apply(my_function)
res_series.name = 'count'
res_df = res_series.reset_index()
print res_df

这会给你这个DataFrame:

     group     count
1    86        'TP'
2    94        'TP'

【讨论】:

  • @user3478208 这段代码可以在 Pandas 中使用吗?因为我们不必为函数使用“def”吗?
  • 我收到此错误:AttributeError: 'Series' object has no attribute 'iterrows'
  • 谢谢。我仍然收到此错误。TypeError: expected string or buffer
  • 如果你复制我所有的代码并将其粘贴到你的 python 编辑器上,它可以工作并给出我声明的输出。
  • 您尝试使用您的数据集还是我在示例中给出的数据集?因为如果您无法运行我的代码,我确信您的数据集与您向我们展示的数据集不同。
【解决方案2】:

此函数将逐行比较每个提供的组的名称和匹配列:

def apply_func(df):
    x = df['name'] == df['match']
    return x.map({False:'FIN', True:'TP'})

In [683]: temp.join(temp.groupby('group').apply(apply_func).reset_index(), rsuffix='_1', how='left')
Out[683]: 
           match                  name  group  group_1  level_1    0
0        adamant  Adamant Home Network     86       86        0  FIN
1        adamant         ADAMANT, Ltd.     86       86        1  FIN
2  adamant bild       TOV Adamant-Bild     86       86        2  FIN
3       360works              360WORKS     94       94        3  FIN
4       360works          360works.com     94       94        4  FIN

【讨论】:

  • 感谢您的帮助。但这不是我想要的。因为我想比较每个组号的名称行,所以在这种情况下,例如我想比较 360works.com 和 360works 以查看它们是否具有相同的“匹配”
  • 组内,是按行匹配还是按列匹配?
  • 行基础。所以要根据它们的“匹配”字两两匹配第 86 组的三个实体
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-06-22
  • 1970-01-01
  • 1970-01-01
  • 2020-10-23
  • 2020-10-13
  • 1970-01-01
  • 2020-10-30
相关资源
最近更新 更多