我在复杂的举动上遇到麻烦。我有一个图片位置和目的地位置的数据框。我有一个'train / test / val'目录。当前,所有文件根据其类在火车目录中,并且在另一个文件夹中(共28个类)。
我需要将每个班级的10%从train / class目录移动到test / class目录和val / class目录,而将80%的文件保留在它们所在的位置。
我使用以下代码将所有文件从一个固定文件夹移至其类文件夹:
import shutil
import os
if __name__ == '__main__':
for index, row in labels.iterrows():
try:
shutil.move(row['location'], row['destination'])
except:
pass
我在墙上碰壁,想出如何根据文件类别移动一定数量的文件。
以下是每个类的数据帧,列count
中的文件数,以及要移至move_count
中的测试目录和验证目录的文件数。
import pandas as pd
dic = {'class': ['other_hazard', 'roof', 'front_of_dwelling',
'address_verification', 'rear_of_dwelling',
'left_front_of_dwelling', 'right_front_of_dwelling',
'detached_structure', 'roof_hazard', 'right_side_of_dwelling',
'left_side_of_dwelling', 'other', 'left_rear_of_dwelling',
'right_rear_of_dwelling', 'restricted_access', 'opportunity_line',
'dog_on_premises', 'pool', 'adjacent_exposure', 'apparent_feature',
'basement', 'hot_tub', 'utl_utc',
'supplemental_heating_source_(wood_stove)', 'brush',
'electrical_panel', 'street_sign_(utl)', 'business_exposure'], 'count': [643612, 631651, 441595, 436153, 365357, 275534, 275023, 220917,
194744, 110098, 109182, 59484, 46058, 45358, 21599, 13753,
5668, 4586, 3292, 3197, 2899, 2873, 2862, 1994,
1677, 1392, 1077, 1063], 'move_count': [64361., 63165., 44160., 43615., 36536., 27553., 27502., 22092.,
19474., 11010., 10918., 5948., 4606., 4536., 2160., 1375.,
567., 459., 329., 320., 290., 287., 286., 199.,
168., 139., 108., 106.]}
df_class = pd.DataFrame(dic)
下面是数据框的示例,其中列出了文件所在的位置以及需要将文件的百分比移动到的位置。
df = {'PictureFilename': ['0091240758-91931712.JPG','0091240791-91646592.JPG', '0091240791-91646593.JPG']
, 'Target': ['Detached Structure','Address Verification', 'Other']
, 'location': ['D:\\CIS inspection images 0318\\Photos\\train\\0091240758-91931712.JPG'
,'D:\\CIS inspection images 0318\\Photos\\train\\0091240791-91646592.JPG'
,'D:\\CIS inspection images 0318\\Photos\\train\\0091240791-91646593.JPG']
,'test_move_location' : ['D:\\CIS inspection images 0318\\Photos\test\detached_structure'
,'D:\\CIS inspection images 0318\\Photos\\test\\address_verification'
,'D:\\CIS inspection images 0318\\Photos\\test\\other']
, 'val_move_location': ['D:\\CIS inspection images 0318\\Photos\\val\\detached_structure'
,'D:\\CIS inspection images 0318\\Photos\\val\\address+verification'
,'D:\\CIS inspection images 0318\\Photos\\val\\other']
}
df_1 = pd.DataFrame.from_dict(df)
我无法使用一个DF来引导另一个并执行此动作。有什么建议么?我尝试过仅使用打印语句启动的内容,似乎已挂断电话。我什至不确定它是否在正确的班级中保持正确的计数:
for index, row in df_1.iterrows():
for i, r in df_class.iterrows():
if row['target'] == r['class']:
for i in range(1, r['move_count']):
print(i, row['target'], r['class'])
好吧,我找到了一种遍历数据框并打印文件位置和目标位置的方法,这意味着我可以移动它们。我无法弄清楚如何停止一定数量的文件的移动。
categories = df_class['class'].unique()
for category in categories:
n = 0
for index, row in labels.iterrows():
if category == row['target']:
n += 1
print(n, train_dir+'\\'+row['target']+'\\'+row['PictureFilename'], 'destination: '+ test_dir+'\\'+row['target']+'\\'+row['PictureFilename'])
答案 0 :(得分:0)
经过数小时的尝试,我找到了答案。好吧,一个答案。可能有一些更有效的方法。
#move 10% to test folder
for i, r in label_move_count.iterrows():
n=0
for index, row in labels.iterrows():
if row['target'] == r['target']:
#n+=1
if n < row['testcount']:
try:
shutil.move(row['destination']+'\\'+row['PictureFilename'], row['test_destination'])
print(n, row['target'])
n+=1
except:
pass
else:
pass
我运行了两次,也将10%移到了验证文件夹。