简体   繁体   English

如何字符串比较熊猫数据框中的两列?

[英]how to string compare two columns in pandas dataframe?

I have a dataframe df that looks like this: 我有一个看起来像这样的数据框df:

        a      b
  0     Jon     Jon
  1     Jon     John
  2     Jon     Johnny

And I'd like to compare these two strings to and make a new column like such: 我想将这两个字符串进行比较,然后创建一个新的列,例如:

  df['compare'] = df2['a'] = df2['b']


        a      b          compare
  0     Jon     Jon         True
  1     Jon     John        False
  2     Jon     Johnny      False

I'd also like to be able to pass columns a and b through this levenshtein function: 我还希望能够通过此levenshtein函数传递列a和b:

def levenshtein_distance(a, b):
    """Return the Levenshtein edit distance between two strings *a* and *b*."""
    if a == b:
        return 0
    if len(a) < len(b):
        a, b = b, a
    if not a:
        return len(b)
    previous_row = range(len(b) + 1)
    for i, column1 in enumerate(a):
        current_row = [i + 1]
        for j, column2 in enumerate(b):
            insertions = previous_row[j + 1] + 1
            deletions = current_row[j] + 1
            substitutions = previous_row[j] + (column1 != column2)
            current_row.append(min(insertions, deletions, substitutions))
        previous_row = current_row
    return previous_row[-1] 

and add a column like such: 并添加如下所示的列:

  df['compare'] = levenshtein_distance(df2['a'], df2['b'])      

        a      b          compare
   0    Jon     Jon         100
   1    Jon     John        .95
   2    Jon     Johnny      .87

However I am getting this error when I try: 但是,当我尝试时出现此错误:

  ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

How can I format my data/dataframe to allow it to compare the two columns and add taht comparison as a third column? 如何格式化我的数据/数据框以允许它比较两列,并将比较添加为第三列?

Just do: 做就是了:

df['compare'] = [levenshtein_distance(a, b) for a, b in zip(df2['a'], df2['b'])]

Or, if you want equality comparison: 或者,如果要进行相等比较:

df['compare'] = (df['a'] == df['b'])

I think you compares are wrong, change: 我认为您比较是错误的,请更改:

change: 更改:

if a == b

and not a

to

if a[0] == b[0]

and 

not a[0]

and you'll see that your function works, it just needs to iterate through the df's you pass. 并且您会看到函数可以正常工作,只需要迭代通过的df即可。 And your equal will return if you return a list 如果返回列表,则等于将返回

Here's a working version: 这是一个工作版本:

def levenshtein_distance(a, b):
  """Return the Levenshtein edit distance between two strings *a* and *b*."""
  y = len(a)
  thelist = []
  for x in range(0, y):
    c = a[x]
    d = b[x] 
    if c == d:
        thelist.append(0)
        continue
    if len(c) < len(d):
        c, d = d, c
    if not c:
        thelist.append(len(d))
        continue
    previous_row = range(len(d) + 1)
    for i, column1 in enumerate(c):
        current_row = [i + 1]
        for j, column2 in enumerate(d):
            insertions = previous_row[j + 1] + 1
            deletions = current_row[j] + 1
            substitutions = previous_row[j] + (column1 != column2)
            current_row.append(min(insertions, deletions, substitutions))
        previous_row = current_row
    thelist.append(previous_row[-1])
  return thelist
df['compare'] =  levenshtein_distance(df.a, df.b)                                                                                                                

df                                                                                                                                                               

#     a       b  compare
#0  Jon     Jon        0
#1  Jon    John        1
#2  Jon  Johnny        3

It just doesn't calculate the percentages, it just uses your code, which according to Levenshtein Calc is the right answers 它只是不计算百分比,仅使用您的代码,根据Levenshtein Calc ,这是正确的答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM