Python 合并两个具有相同索引的 Pandas 数据帧
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28773683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:44:08 来源:igfitidea点击:
Combine two Pandas dataframes with the same index
提问by alexsalo
I have two dataframes with the same index but different columns. How do I combine them into one with the same index but containing all the columns?
我有两个具有相同索引但不同列的数据框。如何将它们合并为一个具有相同索引但包含所有列的?
I have:
我有:
A
1 10
2 11
B
1 20
2 21
and I need the following output:
我需要以下输出:
A B
1 10 20
2 11 21
采纳答案by BrenBarn
pandas.concat([df1, df2], axis=1)
回答by andrewwowens
You've got a few options depending on how complex the dataframe is:
根据数据框的复杂程度,您有几个选项:
Option 1:
选项1:
df1.join(df2, how='outer')
Option 2:
选项 2:
pd.merge(df1, df2, left_index=True, right_index=True, how='outer')