Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将两个具有相同索引的数据帧组合在一起_Python_Pandas - Fatal编程技术网

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')