I have two dataframes and each one has two index columns. I would like to merge them. For example, the first dataframe is the following:
我有两个数据帧,每个数据帧有两个索引列。我想合并它们。例如,第一个数据帧如下:
V1
A 1/1/2012 12
2/1/2012 14
B 1/1/2012 15
2/1/2012 8
C 1/1/2012 17
2/1/2012 9
The second dataframe is the following:
第二个数据帧如下:
V2
A 1/1/2012 15
3/1/2012 21
B 1/1/2012 24
2/1/2012 9
D 1/1/2012 7
2/1/2012 16
and as result I would like to get the following:
结果我想得到以下内容:
V1 V2
A 1/1/2012 12 15
2/1/2012 14 N/A
3/1/2012 N/A 21
B 1/1/2012 15 24
2/1/2012 8 9
C 1/1/2012 7 N/A
2/1/2012 16 N/A
D 1/1/2012 N/A 7
2/1/2012 N/A 16
I have tried a few versions using the pd.merge
and .join
methods, but nothing seems to work. Do you have any suggestions?
我使用pd.merge和.join方法尝试了几个版本,但似乎没有任何工作。你有什么建议吗?
2 个解决方案
#1
33
You should be able to use join
, which joins on the index as default. Given your desired result, you must use outer
as the join type.
您应该能够使用join,它默认连接索引。根据您所需的结果,您必须使用outer作为连接类型。
>>> df1.join(df2, how='outer')
V1 V2
A 1/1/2012 12 15
2/1/2012 14 NaN
3/1/2012 NaN 21
B 1/1/2012 15 24
2/1/2012 8 9
C 1/1/2012 17 NaN
2/1/2012 9 NaN
D 1/1/2012 NaN 7
2/1/2012 NaN 16
Signature: _.join(other, on=None, how='left', lsuffix='', rsuffix='', sort=False) Docstring: Join columns with other DataFrame either on index or on a key column. Efficiently Join multiple DataFrame objects by index at once by passing a list.
签名:_. join(其他,on = None,how ='left',lsuffix ='',rsuffix ='',sort = False)Docstring:在索引或键列上与其他DataFrame连接列。通过传递列表,有效地通过索引连接多个DataFrame对象。
#2
15
You can do this with merge
:
您可以通过合并执行此操作:
df_merged = df1.merge(df2, how='outer', left_index=True, right_index=True)
The keyword argument how='outer'
keeps all indices from both frames, filling in missing indices with NaN
. The left_index
and right_index
keyword arguments have the merge be done on the indices. If you get all NaN
in a column after doing a merge, another troubleshooting step is to verify that your indices have the same dtypes
.
关键字参数how ='outer'保留两个帧的所有索引,用NaN填充缺失的索引。 left_index和right_index关键字参数在索引上进行合并。如果在合并后在列中获得所有NaN,则另一个故障排除步骤是验证索引是否具有相同的dtypes。
The merge
code above produces the following output for me:
上面的合并代码为我生成以下输出:
V1 V2
A 2012-01-01 12.0 15.0
2012-02-01 14.0 NaN
2012-03-01 NaN 21.0
B 2012-01-01 15.0 24.0
2012-02-01 8.0 9.0
C 2012-01-01 17.0 NaN
2012-02-01 9.0 NaN
D 2012-01-01 NaN 7.0
2012-02-01 NaN 16.0
#1
33
You should be able to use join
, which joins on the index as default. Given your desired result, you must use outer
as the join type.
您应该能够使用join,它默认连接索引。根据您所需的结果,您必须使用outer作为连接类型。
>>> df1.join(df2, how='outer')
V1 V2
A 1/1/2012 12 15
2/1/2012 14 NaN
3/1/2012 NaN 21
B 1/1/2012 15 24
2/1/2012 8 9
C 1/1/2012 17 NaN
2/1/2012 9 NaN
D 1/1/2012 NaN 7
2/1/2012 NaN 16
Signature: _.join(other, on=None, how='left', lsuffix='', rsuffix='', sort=False) Docstring: Join columns with other DataFrame either on index or on a key column. Efficiently Join multiple DataFrame objects by index at once by passing a list.
签名:_. join(其他,on = None,how ='left',lsuffix ='',rsuffix ='',sort = False)Docstring:在索引或键列上与其他DataFrame连接列。通过传递列表,有效地通过索引连接多个DataFrame对象。
#2
15
You can do this with merge
:
您可以通过合并执行此操作:
df_merged = df1.merge(df2, how='outer', left_index=True, right_index=True)
The keyword argument how='outer'
keeps all indices from both frames, filling in missing indices with NaN
. The left_index
and right_index
keyword arguments have the merge be done on the indices. If you get all NaN
in a column after doing a merge, another troubleshooting step is to verify that your indices have the same dtypes
.
关键字参数how ='outer'保留两个帧的所有索引,用NaN填充缺失的索引。 left_index和right_index关键字参数在索引上进行合并。如果在合并后在列中获得所有NaN,则另一个故障排除步骤是验证索引是否具有相同的dtypes。
The merge
code above produces the following output for me:
上面的合并代码为我生成以下输出:
V1 V2
A 2012-01-01 12.0 15.0
2012-02-01 14.0 NaN
2012-03-01 NaN 21.0
B 2012-01-01 15.0 24.0
2012-02-01 8.0 9.0
C 2012-01-01 17.0 NaN
2012-02-01 9.0 NaN
D 2012-01-01 NaN 7.0
2012-02-01 NaN 16.0