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

如何将DataFrame列中嵌套的多个DataFrame合并为单个DataFrame?

解决方法

直接提取info列中的所有嵌套DataFrame,用pandas.concat()纵向合并即可——由于所有嵌套DataFrame结构一致,这个方法会自动对齐列,高效完成堆叠。

示例代码

先构造一个模拟的外层DataFrame:

import pandas as pd

# 生成结构相同的嵌套DataFrame
df1 = pd.DataFrame({'col1': [1,2], 'col2': ['a','b']})
df2 = pd.DataFrame({'col1': [3,4], 'col2': ['c','d']})
df3 = pd.DataFrame({'col1': [5,6], 'col2': ['e','f']})

# 外层DataFrame,info列存储嵌套DataFrame
outer_df = pd.DataFrame({'info': [df1, df2, df3]})

执行合并操作:

# 提取info列的所有DataFrame组成列表,再纵向合并
merged_df = pd.concat(outer_df['info'].tolist(), ignore_index=True)

关键说明

  • outer_df['info'].tolist():将info列的每个嵌套DataFrame提取出来,组成一个可被concat处理的列表。
  • pd.concat():默认按行(axis=0)纵向合并,ignore_index=True会重置合并后DataFrame的索引,避免出现重复索引问题。

这种方法比手动循环逐个append效率高得多,因为concat是pandas原生优化的批量合并操作。

内容的提问来源于stack exchange,提问作者NFeruch - FreePalestine

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 18:58:35