如何有效地遍历两个数据帧以比较列和填充数据

问题描述 投票:0回答:1

我具有以下代码来迭代两个数据帧。

for i, row in df1.iterrows():
    for j, innerrow in df2.iterrows():
        if row["df1_id"] == innerrow["df2_id"]:
            df1.at[i,"count_col_df1"] = innerrow["count_col_df2"]

这里,完成ID列的比较以填充df2中df1中一列的数据。由于每个数据帧中有10,000多个记录,因此需要数小时才能完成。任何有关有效方式编译代码的建议都将受到欢迎。在此先感谢

python pandas performance dataframe time-complexity
1个回答
0
投票
如果我对您的理解正确,这将为您提供帮助。 eq()通过检查值是否相等来返回True或False。

df2.loc[df1['df1_id'].eq(df2['df2_id']), 'count_col_df2'] = df['count_col_df1']

© www.soinside.com 2019 - 2024. All rights reserved.