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

如何提取Pandas DataFrame列中列表内的边界框数组?

问题描述

我在Pandas DataFrame的bounding_boxes列中存储了格式为"xy xy"的边界框数据,每个单元格内是包含三个numpy array的列表。想要提取第一行中的这三个数组,输入示例如下:

"[array([     1178.4,      1.6609,      1532.1,      935.91], dtype=float32), array([     761.56,      386.14,      953.42,      936.99], dtype=float32), array([      458.4,      544.93,      1014.9,      943.33], dtype=float32)]"

期望输出:

output:
 a = [     1178.4,      1.6609,      1532.1,      935.91]
 b = [     761.56,      386.14,      953.42,      936.99]
 c = [      458.4,      544.93,      1014.9,      943.33]
解决方案

情况1:单元格存储的是实际numpy数组列表

如果bounding_boxes列的内容是真实的numpy数组列表(而非字符串),直接通过索引提取并转换为普通列表即可:

import pandas as pd
import numpy as np

# 假设你的DataFrame名为df
first_row_boxes = df['bounding_boxes'].iloc[0]
a = first_row_boxes[0].tolist()
b = first_row_boxes[1].tolist()
c = first_row_boxes[2].tolist()

print("output:")
print(f" a = {a}")
print(f" b = {b}")
print(f" c = {c}")

情况2:单元格存储的是字符串格式的数组列表

如果单元格里是字符串形式的数组定义(如输入示例所示),需要先解析字符串再转换:

import pandas as pd
import numpy as np
import ast

# 提取第一行的字符串内容
box_str = df['bounding_boxes'].iloc[0]
# 替换字符串中的array(为np.array(,再用ast.literal_eval解析
box_list = ast.literal_eval(box_str.replace('array(', 'np.array('))
# 转换为普通列表
a = box_list[0].tolist()
b = box_list[1].tolist()
c = box_list[2].tolist()

print("output:")
print(f" a = {a}")
print(f" b = {b}")
print(f" c = {c}")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 07:52:43