对每一行应用 Python 函数并追加

Apply Python function to each row and append

我有以下数据:

我正在尝试使用库 - pyjarowinkler 并找到字符串之间的距离 - 我的 hello world 代码有效

#Hello World
d1=distance.get_jaro_distance("Hello","hello", winkler=True, scaling=0.1);
d1

当我尝试遍历每一行或使用应用程序时,我的代码失败了。有人可以指出我正确的方向吗?

#Import data 
import pandas
df = pandas.read_csv('data.csv')
from pyjarowinkler import distance
score=df.apply(distance.get_jaro_distance(df[S1],df[Stores]))



# iterating over rows using iterrows() function  
for i, j in df.iterrows(): 
    print(i, j,distance.get_jaro_distance(i,j,winkler=True, scaling=0.1)) 
    print()

错误:

JaroDistanceException: Cannot calculate distance from NoneType (int, Series)

预期输出为:

我觉得你应该可以做到

df['distance'] = df.apply(lambda d: distance.get_jaro_distance(d['S1'],d['store'],winkler=True,scaling=0.1), axis=1)

注意传递给 .applyaxis=1 参数,这告诉它在 df 上按行操作而不是按列操作