比较两个数据帧,如果值匹配,则更新一个数据帧

时间:2019-06-27 09:27:56

标签: python pandas performance

我正在比较不同数据框中的两列,如果两列中的值都匹配,则更新数据框中的一列。数据帧很大,程序需要很长时间才能完成运行。

我正在使用一个for循环,该循环遍历第一个数据帧的每一行,获取值并通过一个for循环在另一个数据帧中对其进行搜索。


def Comparison(row):
   for i in dfEquipment.axes[0]:
      if dfClaims.loc[row, 'A'] == dfEquipment.loc[i, 'B']:
         dfClaims.loc[row, 'Result'] = dfEquipment.loc[i, 'Result']



for i in dfClaims.axes[0]:
  Comparison(i)


此代码可以正常工作,但完成时间太长,因为dfEquipment数据帧大约有700,000行,而dfClaims(包含约8000行)列中的每个值都必须遍历整个数据帧。

根据我使用dfClaims中减少的行数进行的粗略计算,每个dfClaims的for循环平均需要大约5秒钟才能运行,如果我使用具有8000行的原始dfClaims数据框,则该程序将需要大约11个小时才能完成。

我想减少时间,以使程序运行不会超过1个小时。

0 个答案:

没有答案
相关问题