【发布时间】:2021-08-12 02:52:07
【问题描述】:
我有两个大数据框(1000行),我需要通过子字符串来匹配它们,例如:
df1:
Id Title
1 The house of pump
2 Where is Andijan
3 The Joker
4 Good bars in Andijan
5 What a beautiful house
df2:
Keyword
house
andijan
joker
预期的输出是:
Id Title Keyword
1 The house of pump house
2 Where is Andijan andijan
3 The Joker joker
4 Good bars in Andijan andijan
5 What a beautiful house house
现在,我写了一种非常低效的方法来匹配它,但是对于数据帧的实际大小,它运行了很长时间:
for keyword in df2.to_dict(orient='records'):
df1['keyword'] = np.where(creative_df['title'].str.contains(keyword['keyword']), keyword['keyword'], df1['keyword'])
现在,我确信有一种更适合 pandas 且更有效的方式来做同样的事情,而且还能在合理的时间内运行。
【问题讨论】:
标签: python pandas performance optimization string-matching