我有两个熊猫数据框,一个具有更新后的值作为主要数据框的一部分值。主要的是〜2m行,要更新的列是〜20k。据我所知,此操作运行非常缓慢,据我所知,它低于O(m * n),有没有很好的方法对其进行矢量化或只是总体上提高了速度?我看不出有多少其他优化可以应用于这种情况。我也尝试过将'object_id'列作为索引,但这并没有导致速度的有意义的提高。
# df_primary this is 2m rows
# df_updated this is 20k rows
for idx, row in df_updated.iterrows():
df_primary.loc[df_primary.object_id == row.object_id, ['status', 'category']] = [row.status, row.category]
让我们尝试使用中的值来就地DataFrame.update
更新:df_primary
df_updated
df_primary = df_primary.set_index('object_id')
df_primary.update(df_updated.set_index('object_id')[['status', 'category']])
df_primary = df_primary.reset_index()
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句