Python 对列表的dataframe列中的每个元素运行函数
这个对我来说有点棘手 数据帧:Python 对列表的dataframe列中的每个元素运行函数,python,pandas,fuzzy-comparison,Python,Pandas,Fuzzy Comparison,这个对我来说有点棘手 数据帧: parent children 0 MAX [MAX, amx, akd] 1 Sam ['Sam','sammy','samsam'] 2 Larry ['lar','lair','larrylamo'] 我有一个函数,如果我只传入一个字符串,它将比较两个字符串,并打印出一个数字,描述字符之间的距离。类似于levenshtein方程 如何在数据帧上运行此函数?我需要将第一列(“父
parent children
0 MAX [MAX, amx, akd]
1 Sam ['Sam','sammy','samsam']
2 Larry ['lar','lair','larrylamo']
我有一个函数,如果我只传入一个字符串,它将比较两个字符串,并打印出一个数字,描述字符之间的距离。类似于levenshtein方程
如何在数据帧上运行此函数?我需要将第一列(“父项”)中的每条记录与第二列(“子项”)中的相应列表进行比较
目前,我可以运行此程序并获得以下结果:
>>> reference = 'larry'
>>> value_list = ['lar','lair','larrylamo']
>>> get_top_matches(reference,value_list)
>>> [('lar',0.91),('larrylamo',0.91),('lair',0.83)]
我试图为每一行匹配创建一个由元组组成的第三列,如下所示:
parent children func_results
0 MAX [MAX, amx, akd] [('MAX',1.0),('amx',0.89),('akd',0.56)]
1 Sam ['Sam','sammy','samsam'] [('Sam',1.0),('sammy',0.91), ('samsam',0.88)]
2 Larry ['lar','lair','larrylamo'] [('lar',0.91),('larrylamo',0.91), ('lair',0.83)]
我认为,如果我能够找出如何在for循环中针对df应用它,那么函数应该能够按原样工作
以下是功能:
import math
import re
def sort_token_alphabetically(word):
token = re.split('[,. ]', word)
sorted_token = sorted(token)
return ' '.join(sorted_token)
def get_jaro_distance(first, second, winkler=True, winkler_ajustment=True,
scaling=0.1, sort_tokens=True):
if sort_tokens:
first = sort_token_alphabetically(first)
second = sort_token_alphabetically(second)
if not first or not second:
raise JaroDistanceException(
"Cannot calculate distance from NoneType ({0}, {1})".format(
first.__class__.__name__,
second.__class__.__name__))
jaro = _score(first, second)
cl = min(len(_get_prefix(first, second)), 4)
if all([winkler, winkler_ajustment]): # 0.1 as scaling factor
return round((jaro + (scaling * cl * (1.0 - jaro))) * 100.0) / 100.0
return jaro
def _score(first, second):
shorter, longer = first.lower(), second.lower()
if len(first) > len(second):
longer, shorter = shorter, longer
m1 = _get_matching_characters(shorter, longer)
m2 = _get_matching_characters(longer, shorter)
if len(m1) == 0 or len(m2) == 0:
return 0.0
return (float(len(m1)) / len(shorter) +
float(len(m2)) / len(longer) +
float(len(m1) - _transpositions(m1, m2)) / len(m1)) / 3.0
def _get_diff_index(first, second):
if first == second:
pass
if not first or not second:
return 0
max_len = min(len(first), len(second))
for i in range(0, max_len):
if not first[i] == second[i]:
return i
return max_len
def _get_prefix(first, second):
if not first or not second:
return ""
index = _get_diff_index(first, second)
if index == -1:
return first
elif index == 0:
return ""
else:
return first[0:index]
def _get_matching_characters(first, second):
common = []
limit = math.floor(min(len(first), len(second)) / 2)
for i, l in enumerate(first):
left, right = int(max(0, i - limit)), int(
min(i + limit + 1, len(second)))
if l in second[left:right]:
common.append(l)
second = second[0:second.index(l)] + '*' + second[
second.index(l) + 1:]
return ''.join(common)
def _transpositions(first, second):
return math.floor(
len([(f, s) for f, s in zip(first, second) if not f == s]) / 2.0)
def get_top_matches(reference, value_list, max_results=None):
scores = []
if not max_results:
max_results = len(value_list)
for val in value_list:
score_sorted = get_jaro_distance(reference, val)
score_unsorted = get_jaro_distance(reference, val, sort_tokens=False)
scores.append((val, max(score_sorted, score_unsorted)))
scores.sort(key=lambda x: x[1], reverse=True)
return scores[:max_results]
class JaroDistanceException(Exception):
def __init__(self, message):
super(Exception, self).__init__(message)
reference = 'larry'
value_list = ['lar','lair','larrylamo']
get_top_matches(reference, value_list)
我假设您的真实数据集有两列作为示例。在轴=1上使用
agg
df['func_results'] = df.agg(lambda x: get_top_matches(*x), axis=1)
Out[366]:
parent ... func_results
0 MAX ... [(MAX, 1.0), (amx, 0.89), (akd, 0.56)]
1 Sam ... [(Sam, 1.0), (sammy, 0.87), (samsam, 0.83)]
2 Larry ... [(lar, 0.87), (larrylamo, 0.85), (lair, 0.78)]
[3 rows x 3 columns]
你的功能是什么,你(用代码)尝试了什么,结果是什么?我们要求这个网站上的问题包括一个可以运行和测试的比较函数。你的比较函数是什么样子?@G.Anderson我已经添加了一个可执行版本的函数。@SeyiDaniel刚刚将函数添加到问题中。refreshah,您的
children\u org\u name\u list
列包含表示列表的字符串。也就是说,它是用字符串包装的列表。它不是您描述的字符串列表。您需要将每个字符串转换为字符串列表,因为函数需要的是字符串列表,而不是列表的字符串包装。@max:尝试运行此操作以转换为字符串列表:df['children\u org\u name\u list']=df.children\u org\u name\u list.str.strip('[]').str split(',')
我使用了以下命令:df['children org\u name\u list']=df.children\u org\u name\u list.apply(literal\u eval)
它成功了。非常感谢Andy。@max:x
是每行数据帧<代码>*x将每行解压为单独的值。因为你有两列*x
将每行解压为2个值,并将它们传递给获取顶级匹配项
。如果您的数据帧有2列以上,但您只想传递2列以获得匹配项,则需要切片2列,例如:df[['col1','col2']]].agg(λx:get\u top\u matches(*x),axis=1)
假设col1
和col2
是要将值传递给get\u top\u匹配项的两列
@max:convertcol2
到列表列:df['col2']=df['col2'].agg(list)
。此命令将col2
的每个单元格转换为一个字符串列表。