熊猫大熊猫的替代品

时间:2020-08-27 14:51:11

标签: python pandas

我正在尝试将两个数据帧连接起来

pd.concat([df1.set_index(["t", "tc"]), df2.set_index(["t", "tc"])], axis=1)

在df1中,索引可能不是唯一的。在这种情况下,我希望将df2中的相应条目插入具有该索引的所有行中。不幸的是,concat没有给我一个错误。我认为ignore_index = True可能会有所帮助,但我仍然收到错误ValueError: cannot handle a non-unique multi-index! 除了concat之外,还有其他我想要的东西吗?

例如: df1

t  tc a
a  1  5
b  1  6
a  1  7

df2:

t tc b
a 1  8
b 1  10

结果(重置索引后):

t tc a b
a 1  5 8
b 1  6 10
a 1  7 8

1 个答案:

答案 0 :(得分:1)

使用.merge可以到达需要的地方

df1.merge(df2, on =['t', 'tc'])
#result
    t   tc  a   b
0   a   1   5   8
1   a   1   7   8
2   b   1   6   10