我有以下问题:我想连接或合并两个长度不同、索引部分不同的数据帧:
数据1:
索引 | data1 |
---|---|
1 | 16 |
2 | 37 |
3 | 18 |
7 | 49 |
您可以使用join
:
out = df1.join(df2, how='outer')
print(out)
# Output
data1 data2
index
1 16.0 NaN
2 37.0 74.0
3 18.0 86.0
4 NaN 12.0
6 NaN 97.0
7 49.0 NaN
12 NaN 35.0
或者您可以使用merge
:
out = df1.merge(df2, left_index=True, right_index=True, how='outer')
或者您可以使用concat
:
out = pd.concat([df1, df2], axis=1).sort_index()