python - 在 Python 中合并/组合 2 个数据框,将数据框 1 中的行替换为数据框 2 中 2 列中的重复值
问题描述
我有 2 个要合并为 1 个的数据帧。2 个数据帧中的第二个,我想用相同的 'x' 和 'y' 值覆盖行。我还想用数据框 2 中的“人口”值覆盖这些行。
我尝试了以下方法,但它似乎没有提供我需要的东西。
df_out = df2.merge(same,
on=['x', 'y'],
how='outer', indicator=True)
df_out['population'] = np.where(df_out['_merge'] == 'both',
df_out['population_x'],
df_out
.population_y
.add(df_out.population_x, fill_value=0))\
.astype(int)
df_out[['population', 'x', 'y']]
每一步之后的df_out:
抱歉,如果我没有包含所需的信息或给出正确的格式,这是我发布的第一个问题。非常感谢任何帮助!