所以我有大约65000张汽车的jpg图片,每个文件名都有关于汽车的信息。例如:
Acura_ILX_2013_28_16_110_15_4_70_55_179_39_FWD_5_4_4dr_aWg.jpg
'Displacement', 'Engine Type', 'Width, Max w/o mirrors (in)', 'Height, Overall (in)',
'Length, Overall (in)', 'Gas Mileage', 'Drivetrain', 'Passenger Capacity', 'Passenger Doors',
'Body Style' 'unique identifier'
因为同一辆车有不同的图像,所以在每个文件的末尾使用一个唯一的3个字母的标识符
我使用以下代码从文件名创建了一个数据帧:
car_file = os.listdir(r"dir")
make = []
model = []
year = []
msrp = []
front_wheel_size = []
sae_net_hp = []
displacement = []
engine_type = []
width = []
height = []
length = []
mpg = []
drivetrain = []
passenger_capacity = []
doors = []
body_style = []
for i in car_file:
make.append(i.split("_")[0])
model.append(i.split("_")[1])
year.append(i.split("_")[2])
msrp.append(i.split("_")[3])
front_wheel_size.append(i.split("_")[4])
sae_net_hp.append(i.split("_")[5])
displacement.append(i.split("_")[6])
engine_type.append(i.split("_")[7])
width.append(i.split("_")[8])
height.append(i.split("_")[9])
length.append(i.split("_")[10])
mpg.append(i.split("_")[11])
drivetrain.append(i.split("_")[12])
passenger_capacity.append(i.split("_")[13])
doors.append(i.split("_")[14])
body_style.append(i.split("_")[15])
df = pd.DataFrame([make,model,year,msrp,front_wheel_size,sae_net_hp,displacement,engine_type,width,height,length,mpg,drivetrain,passenger_capacity,doors,body_style]).T
(我认为这不是最干净的方法)
我的问题是,我如何才能最有效地将jpg图像包括在数据集中,也许在最后作为一个附加列
我不确定你是否真的想一次打开所有65000张图片,因为这可能会占用大量内存。 我建议只将图像的路径保存在数据帧中
如果确实要打开它,请参见:How to read images into a script?
但要清理原始代码: 不久前我做了类似的事情,我通过正则表达式解决了这个问题。但这可能有点过头了。但是您可以直接使用split将值放入行中,而不是构建列。 下面示例中的两个想法(可能包含错误)
相关问题 更多 >
编程相关推荐