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

将嵌套元组列表转换为指定结构的DataFrame

将嵌套列表转换为指定格式的DataFrame

原始数据

mylist=[((10, 'L5', ['abc']), 0),
 ((15, 'L6', ['bda', 'LAS']), 5)]

实现代码

用pandas可以快速把这个嵌套结构转换成目标格式的DataFrame,代码如下:

import pandas as pd

# 把嵌套列表扁平化,生成每行的四个字段
flattened_data = [[item[0][0], item[0][1], item[0][2], item[1]] for item in mylist]
# 创建DataFrame并指定列名
df = pd.DataFrame(flattened_data, columns=['COL1', 'COL2', 'COL3', 'COL4'])

输出结果

执行代码后得到的DataFrame格式如下:

COL1COL2COL3COL4
10L5['abc']0
15L6['bda', 'LAS']5

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 16:58:56