Python 在给定索引位置的数据帧上单击

Python 在给定索引位置的数据帧上单击,python,pandas,Python,Pandas,我有一个带有多索引的数据帧,还有一个行向量,我想将其值连接到数据帧。这些列事先不存在于第一个数据帧上。例如: # 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 app

我有一个带有多索引的数据帧,还有一个行向量,我想将其值连接到数据帧。这些列事先不存在于第一个数据帧上。例如:

# 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
我知道要将第二个数据帧添加到的索引(但不是行号):
index=(boston,big,bns)
。所以我试着做:

first_dataframe[index, second_dataframe.columns] = second_dataframe
但是我得到了一个
KeyError
,因为
第二个数据帧中的列在第一个数据帧中还不存在。我认为
merge
join
可能是正确的,但它们需要共享索引
concat看起来最正确,但我不知道如何指定第一个数据帧中的哪个
行应该得到第二个

我期望的输出是:

city       animal   zoo   apple  banana   ...  grape
boston     pig      bns   0   5      10   ...  37
new york   tiger    nycz  NaN NaN    NaN  ...  NaN
[...]     

首先在df1中创建列,然后通过
.values
loc

index = ('boston', 'pig', 'bns')
df1=df1.assign(**dict(zip(df2.columns,[np.nan]*df2.shape[1])))
df1.loc[index,df2.columns]=df2.values
df1
                     apple  banana  grape
city    animal zoo                       
boston  pig    bns     5.0    10.0   37.0
newyork tiger  nycz    NaN     NaN    NaN
LOL     L      L       NaN     NaN    NaN
LOL1    L1     L1      NaN     NaN    NaN