Python 将两个具有相同索引的数据帧组合在一起
我有两个索引相同但列不同的数据帧。如何将它们合并为一个索引相同但包含所有列的索引 我有:Python 将两个具有相同索引的数据帧组合在一起,python,pandas,Python,Pandas,我有两个索引相同但列不同的数据帧。如何将它们合并为一个索引相同但包含所有列的索引 我有: A 1 10 2 11 B 1 20 2 21 我需要以下输出: A B 1 10 20 2 11 21 根据数据帧的复杂程度,您有几个选项: 备选案文1: df1.join(df2, how='outer') 备选案文2: pd.merge(df1, df2, left_index=True, right_index=True, how='outer') 为什么在pd.merg
A
1 10
2 11
B
1 20
2 21
我需要以下输出:
A B
1 10 20
2 11 21
根据数据帧的复杂程度,您有几个选项: 备选案文1:
df1.join(df2, how='outer')
备选案文2:
pd.merge(df1, df2, left_index=True, right_index=True, how='outer')
为什么在pd.merge上使用pd.concat?@LiamFoley:
merge
用于连接样式的操作。如果您只想连接数据帧,这就是concat
的用途。
pd.merge(df1, df2, left_index=True, right_index=True, how='outer')