I am trying to create a new column by applying a function (jaccard string distance) between two columns of a pandas dataframe. My dataframe has around 400 million rows.
df['sim_scr'] = df.apply(lambda x: jaccard(x[0],x[1],2),axis=1)
Owing to the huge size, it is taking quite some time, so i tried splitting the dataframe to chunks, apply the function and join them back. Even this is taking very large time:
for chunk in np.array_split(df,1000):
chunk['sim_scr'] = chunk.apply(lambda x: jaccard(x[0],x[1],2),axis=1)
df2 = df2.append(chunk)
Is there any efficient way to achieve this?
EDIT:
This is how i defined my Jaccard distance function:
def jaccard(a,b,n):
s = [a[i:i+n] for i in range(len(a)-n+1)]
t = [b[i:i+n] for i in range(len(b)-n+1)]
if len(list(set(s) | set(t))) >0:
jac_coeff = 1 - len(list(set(s) & set(t))) / len(list(set(s) | set(t)))
else:
jac_coeff = 1
return jac_coeff
Sample Dataframe:
nm1 nm2
John A Smith K
California Cadifornia
San Frans San Fransisco