python - 基于列表、python 的 csv 中的新列

标签 python linux list csv

我有一个 csv 文件 (VV_AL_3T3_P3.csv),每个 csv 文件的每一行都对应于浮游生物的 tiff 图像。它看起来像这样:

Particle_ID  Diameter  Image_File                   Lenght ....etc
          1     15.36  VV_AL_3T3_P3_R3_000001.tif    18.09
          2     17.39  VV_AL_3T3_P3_R3_000001.tif    19.86
          3     17.21  VV_AL_3T3_P3_R3_000001.tif    21.77
          4      9.42  VV_AL_3T3_P3_R3_000001.tif     9.83

所有图像都放在一个文件夹中,然后在文件夹中按形状分类。 tiff 图像的名称由 Image_file + Particle ID 组成;例如第一行:VV_AL_3T3_P3_R3_000001_1.tiff

现在,我想使用 python 在我已有的 csv 文件 (VV_AL_3T3_P3.csv) 中添加一个名为“Class”的新列,其中包含每个 .tiff 文件所在的文件夹的名称(类);像这样:

Particle_ID  Diameter  Image_File                   Lenght   Class
          1     15.36  VV_AL_3T3_P3_R3_000001.tif    18.09   Spherical
          2     17.39  VV_AL_3T3_P3_R3_000001.tif    19.86   Elongated
          3     17.21  VV_AL_3T3_P3_R3_000001.tif    21.77   Pennates
          4      9.42  VV_AL_3T3_P3_R3_000001.tif     9.83   Others

到目前为止,我有一个列表,其中包含每个 tiff 文件所在的文件夹的名称。这是将成为新列的列表。但是,我该怎么做才能使每个文件夹都适合它的行?换句话说,将“类”与“粒子 ID”和“图像文件”相匹配。

现在:

## Load modules:
import os
import pandas as pd
import numpy as np
import cv2

## Function to recursively list files in dir by extension
def file_match(path,extension):
    cfiles = []
    for root, dirs, files in os.walk('./'):
        for file in files:
            if file.endswith(extension):
                cfiles.append(os.path.join(root, file))
    return cfiles


## Load all image file at all folders:
image_files = file_match(path='./',extension='.tiff')

## List of directories where each image was found:
img_dir = [os.path.dirname(one_img)[2:] for one_img in image_files]
len(img_dir)

## List of images:
# Image file column in csv files:
img_file = [os.path.basename(one_img)[:22] for one_img in image_files]
len(img_file)
# Particle id column in csv files:
part_id  = [os.path.basename(one_img)[23:][:-5] for one_img in image_files]
len(part_id)

## I have the information related with the collage picture, particle id and the classification folder.
# Now i need to create a loop where this information is merged...

## Load csv file:
data = pd.read_csv('VV_AL_3T3.csv')
sample_file = data['Image File']  # Column name
sample_id   = data['Particle ID'] # Particle ID

我在这里看到过类似的案例:Create new column in dataframe with match values from other dataframe

但我真的不知道如何使用“map.set_index”,而且他有两个数据框而我只有一个。

最佳答案

对于你问题的第一部分,使用 os.path.split

如果您的路径是.../home/usuario/Desktop/Classification/Fraction_9to20um/Classes/test

os.path.split(path)[1]

将返回测试。

然后在你的 for 循环中,将其附加到每一行

for row in rows:
    row = row.append(os.path.split(path)[1]
    writer.writerow(row)

引用:https://docs.python.org/3/library/os.path.html

关于python - 基于列表、python 的 csv 中的新列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49943206/

相关文章:

python - 从类中获取/循环变量树

linux - ghc 不工作因为缺少 "Rts.h"

ruby - 如何在 Ruby 中返回数组的一部分?

python - 计算嵌套列表中的所有元组元素

python - 如何在 Python 中创建 pickle-friend 对象 ID?

Python:在 setuptools 的 setup.py 中进行行为集成

python - 在 Python 中进行法语文本分析的最佳方法是什么?

linux - 如何在 Mac 上添加 apt-repository ppa

php - Nginx 不提供 PHP 文件

Python - 以非线性方式运行一个循环