我有一个带有multindex的dataframe,还有一个行向量,我想将其值连接到dataframe。列预先不存在于第一个数据帧上。例如:
# First dataframe, lots of rows, index on (city, animal, zoo)
city animal zoo
boston pig bns
new york tiger nycz
[...]
# Second dataframe, one row, non-label index
apple banana ... grape
0 5 10 ... 37
I
知道
要将第二个数据帧添加到的索引(但不是行号):
index = (boston, big, bns)
.所以我试着做:
first_dataframe[index, second_dataframe.columns] = second_dataframe
但我得到了
KeyError
因为
second_dataframe
第一个还不存在。我想
merge
或
join
可能是正确的,但它们需要共享索引。
concat
看起来很正确,但我不知道该怎么说
which
第一个数据框中的行应该得到第二个数据框。
我想要的输出是:
city animal zoo apple banana ... grape
boston pig bns 0 5 10 ... 37
new york tiger nycz NaN NaN NaN ... NaN
[...]