Python 如何连接两个数据帧
我有两个数据帧:Python 如何连接两个数据帧,python,python-2.7,pandas,dataframe,Python,Python 2.7,Pandas,Dataframe,我有两个数据帧: 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合并这两个数据帧。换句话说,我想在df1中添加列comment。我该怎么做?我知道join命令,但它的工作原理不同(df1.join(df2)
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
合并这两个数据帧。换句话说,我想在df1
中添加列comment
。我该怎么做?我知道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
使用并传递要合并的列,默认情况下,这将执行“内部”合并:
In [198]:
df1.merge(df2, on='frid')
Out[198]:
id frid title comment
0 1 1 abc w
1 2 1 ddd w
2 5 1 def w
3 3 2 ghc s
4 4 3 frg e