You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Pandas将两个DataFrame进行垂直合并?

Pandas垂直合并两个DataFrame的方法

你需要将两个结构一致的DataFrame进行垂直拼接(行方向合并),使用Pandas的concat()方法即可快速实现。

步骤示例:

  1. 导入Pandas并创建示例DataFrame
import pandas as pd

df1 = pd.DataFrame([[1, 4], [2, 5], [3, 6]])
df2 = pd.DataFrame([[7, 10], [8, 11], [9, 12]])
  1. 执行垂直合并
# 合并时重置索引,避免索引重复
merged_df = pd.concat([df1, df2], ignore_index=True)
print(merged_df)

输出结果:

0   1
0  1   4
1  2   5
2  3   6
3  7  10
4  8  11
5  9  12

说明:

  • concat()默认按行方向(axis=0)合并,只要两个DataFrame的列结构(列数、列名)一致就能直接使用。
  • ignore_index=True参数会重新生成连续的索引,避免合并后出现重复索引的问题。

内容的提问来源于stack exchange,提问作者Python_Newbie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 08:15:31