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

如何从Pandas DataFrame的filename列提取图片并关联findings列

解决方案
  • 第一步:提取目标列
    先从原DataFrame中筛选出filename和findings列,聚焦需要处理的数据:
# 假设原DataFrame名为df
df_target = df[['filename', 'findings']].copy()
  • 第二步:遍历filename列匹配图片
    基于指定的图片根路径,遍历筛选后的DataFrame,拼接完整路径并读取图片,同时关联对应的诊断描述:
import os
# 可根据需求替换为PIL库读取图片
import cv2

# 图片根路径
img_root_dir = "/content/drive/MyDrive/iuxray/images/images_normalized"

# 逐行处理DataFrame记录
for index, row in df_target.iterrows():
    # 拼接完整图片路径
    full_img_path = os.path.join(img_root_dir, row['filename'])
    # 先校验图片是否存在,避免报错
    if os.path.isfile(full_img_path):
        # 读取图片(用PIL的话替换为 Image.open(full_img_path))
        image = cv2.imread(full_img_path)
        # 获取对应诊断内容
        corresponding_findings = row['findings']
        # 这里添加你的后续处理逻辑(比如存储数据、输入模型等)
        # ...
    else:
        print(f"警告:未找到图片 {full_img_path}")
  • 可选:批量生成图片-诊断对
    如果不需要逐行即时处理,也可以直接生成包含有效图片路径和对应诊断的列表:
img_findings_pairs = [
    (os.path.join(img_root_dir, fn), find)
    for fn, find in zip(df_target['filename'], df_target['findings'])
    if os.path.isfile(os.path.join(img_root_dir, fn))
]

这种方式的核心优势是完全基于DataFrame的记录匹配图片,不会处理路径下无对应诊断记录的多余图片,确保数据一一对应。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 22:40:15