如何连接两个数据帧

2024-09-19 23:27:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个数据帧:

df1
    id frid title
    1  1    abc
    2  1    ddd
    3  2    ghc
    4  3    frg
    5  1    def

df2
    frid comment
    1    w
    2    s
    3    e

现在我想基于frid字段合并这两个数据帧。换句话说,我想将列comment添加到df1。我该怎么做?我知道join命令,但它的工作方式不同(df1.join(df2))。你知道吗

结果应该是:

df
    id frid title comment
    1  1    abc   w
    2  1    ddd   w
    3  2    ghc   s
    4  3    frg   e
    5  1    def   w

Tags: 数据命令idtitledef方式commentdf1