连接两个表但保持索引顺序

问题描述 投票:0回答:1

我有两个具有相同列名的表。我想合并它们,但需要保持索引的顺序:

enter image description here

enter image description here

合并的顺序非常重要,因为我需要 df1 的索引 0 为第一个,df2 的索引 0 为第二个,依此类推。我想要的结果如下: enter image description here

这是为了创建一个excel文件来集成到SAP中,所以顺序非常重要。你能帮我找到正确的合并吗?

我尝试了不同的合并,但找不到尊重索引顺序的合并。

python join indexing merge
1个回答
0
投票

您可以

pd.concat()
两个数据帧并对索引进行排序作为下一步:

out = pd.concat([df1, df2]).sort_index(kind="stable")
print(out)

打印:

       x   y
0    one   1
0    six   6
1    two   2
1  seven   7
2  three   3
2  eight   8
3   four   4
3   nine   9
4   five   5
4    ten  10
© www.soinside.com 2019 - 2024. All rights reserved.