我有一个csv文件(VV\u AL\u 3T3\u 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图像的名称由图像\文件+粒子ID组成;例如,对于第一行:VV\ u AL\ u 3T3\ u P3\ u R3\ u 000001\ u 1.tiff
现在,我想在csv文件(VV\u AL\u 3T3\u P3.csv)中添加一个名为“Class”的新列,其中包含每个.tiff文件所在的文件夹的名称(类),使用python;如下所示:
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文件所在的文件夹的名称。这是将成为新列的列表。但是,如何使每个文件夹都适合其行?换句话说,将“Class”与“Particle ID”和“Image file”匹配。你知道吗
目前:
## 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\u索引而且,他有两个数据帧,而我只有一个。你知道吗
听起来像我的文件是一个列表(路径+tiff文件名)。你想要的是父目录绝对路径的最后一段,看起来。你知道吗
因此,
/some/path/to/directory/classA/instance.tiff
将被赋予classA
。你知道吗有两种方法,有两种稍有不同的解释
1)路径的最后一部分是类。你知道吗
rows = [file.split(os.path.sep)[-2] for file in my_files]
2)相对于
Classes
目录,文件的包含目录是类。你知道吗rows = [ os.path.relpath( os.path.dirname(file), '/home/usuario/Desktop/Classification/Fraction_9to20um/Classes/' ) for file in my_files ]
编辑(用于说明/示例):为了写出类及其文件
你的问题不清楚你是否想要你的
output_path
类.csv或者VV\u AL\u 3T3\u P3.csv,但希望您看到它很容易互换。你知道吗请注意,如果输入和输出之间存在一对一的对应关系(输入->;简单转换->;输出),则上述模式很容易实现/调试。但是,一旦开始聚合数据(例如,每个类的平均文件数),您可能希望开始探索像pandas这样的数据操作库。你知道吗
在问题的第一部分,使用操作系统路径拆分你知道吗
如果你的道路是。。。/家庭/用户/桌面/分类/分数到20um/类/测试
将返回测试。你知道吗
然后在for循环中,将其附加到每一行
参考号:https://docs.python.org/3/library/os.path.html
可以使用^{} 将路径分为两部分:开始部分和最后一部分,不管是文件还是目录。你知道吗
例如:
如果您有完整的文件路径并想要最后的目录名,请运行此命令两次:
相关问题 更多 >
编程相关推荐