【发布时间】:2020-11-17 11:34:44
【问题描述】:
df1:
number | pid
1 | a
1 | aaa
2 | b
3 | c
3 | chh
df2:
number | pod
1 | axa
2 | bvb
3 | ckc
4 | dld
我想将列 df["pod"] 添加到 df1,由列“number”映射。忽略所有未出现在 df1["number"] 中的 df2["number"]。
最终结果应该是:
number | pid | pod
1 | a | axa
1 | aaa | axa
2 | b | bvb
3 | c | ckc
3 | chh | ckc
我认为“左”连接可以做到这一点,但它会将我的 df.shape 从 542 行增加到 304534 行?合并后有很多行重复,删除那些将我的 df1 缩小到 251 行,而不是给我原来的 542 行,只有一个额外的列?
df_merge = pd.merge(df1, df2, on=["number"], how="left")
【问题讨论】: