【发布时间】:2014-04-09 22:17:58
【问题描述】:
我有两个 DataFrame,df1:
ID value 1
0 5 162
1 7 185
2 11 156
和df2:
ID Comment
1 5
2 7 Yes!
6 11
...我想使用ID 加入,结果如下所示:
ID value 1 Comment
5 162
7 185 Yes!
11 156
真正的 DataFrame 更大并且包含更多列,我基本上想将Comment 列从df2 添加到df1。我尝试使用
df1 = df1.join(df2['Comment'], on='ID')
... 但这只会让我在 df1 中获得一个新的空 Comment 列,例如 .join 以某种方式无法使用 ID 列作为索引。我也试过了
df1 = df1.join(df2['Comment'])
...但是使用默认索引,这两个 DataFrame 之间不匹配(它们也有不同的长度),在错误的地方给了我一个 Comment 值。
我做错了什么?
【问题讨论】: