我有两只熊猫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)=6000
,len(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]的相似性
为循环执行此操作花费了太多时间,但我不知道如何改进我的问题。你知道吗
基本上,像您的例子中那样手动循环是最慢的方法,它不会利用内置的pandas/numpy方法。你知道吗
本文很好地澄清了问题:https://engineering.upside.com/a-beginners-guide-to-optimizing-pandas-code-for-speed-c09ef2c6a4d6
相关问题 更多 >
编程相关推荐