我正在尝试将两个数据帧与
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 7df2:
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发布于 2020-08-27 22:58:58
使用.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 10https://stackoverflow.com/questions/63618649
复制相似问题