如何使用pandas数据帧更快地计算字符串相似度

2024-09-24 16:41:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两只熊猫dataframes,我想从dataframes计算字符串相似性。这是我的代码,但我有一个大问题是我的数据太出价和我的代码花费太多的时间(可能需要7天)。有什么方法可以让我的代码更快?你知道吗

import pandas as pd
import re
import difflib

df_post=pd.read_csv('ptt_run.csv',encoding='utf8',header=0)
df_post=df_post.fillna('null')
df_yahoo=pd.read_csv('yahoo_movie_20180519_test.csv',encoding='utf8',header=0)
df_yahoo=df_yahoo.fillna('null')

for i in range(0,len(df_yahoo)):
    df_post[df_yahoo['yahoo_movie_id'][i]]=0

    for j in range(0,len(df_post)):
        df_post.loc[j, df_yahoo['yahoo_movie_id'][i]]=difflib.SequenceMatcher(None, df_yahoo['yahoo_ch_nosign'][i], df_post['title_nosign'][j]).ratio()

df_post.to_csv('df_score_test.csv', encoding='utf8',index=False)

我的len(df_yahoo)=6000len(df_post)=130000
我想知道df\u yahoo['yahoo\u ch\u nosign'][0]与df\u post['title\u nosign'][0~13000]的相似性与df\u yahoo['yahoo\u ch\u nosign'][6000]与df\u post['title\u nosign'][0~13000]的相似性
为循环执行此操作花费了太多时间,但我不知道如何改进我的问题。你知道吗


Tags: csv代码importdflentitleutf8movie