合并两个具有相同索引的 Pandas 数据帧

Combine two Pandas dataframes with the same index

我有两个索引相同但列不同的数据框。如何将它们组合成具有相同索引但包含所有列的一个?

我有:

  A 
1 10 
2 11

  B
1 20
2 21

我需要以下输出:

  A  B
1 10 20
2 11 21
pandas.concat([df1, df2], axis=1)

根据数据框的复杂程度,您有几个选项:

选项 1:

df1.join(df2, how='outer')

选项 2:

pd.merge(df1, df2, left_index=True, right_index=True, how='outer')