如何在python的mein数据集中删除相同数量的照片

问题描述

嗨,我使用python制作了一个食品数据框架,每个食品有1000张照片。 我想放一些食物的照片,例如ice_cream 100张图片,煎蛋卷100张图片...等)

def get_df_from_file(path,is_valid):   
    img_df = pd.read_csv(path,delimiter='/',header=None,names=['label','name'])
    # Append .jpg extension to all file names
    img_df['name'] = img_df['label'].astype(str) + '/' + img_df['name'].astype(str) + '.jpg'

    # Join extracted values and set is_valid
    img_df['is_valid'] = is_valid
    #img_df=img_df.loc[(img_df['label'] == 'ramen')]
    
    return img_df
 
   
# Load the train set
train_df = get_df_from_file(path/'train.txt',False)
print(train_df.shape)

# Load the validation set
test_df = get_df_from_file(path/'test.txt',True)
print(test_df.shape)

# Concatenate train and test sets
image_df = pd.concat([train_df,test_df])
display(image_df.sample(10))
print(image_df.shape)
train_df0=train_df.loc[(train_df['label'] == df.label[0])]
drop_indices = np.random.choice(train_df0.index,50,replace=False)
train_df0 = train_df0.drop(drop_indices)

train_df1=train_df.loc[(train_df['label'] == df.label[1])]
drop_indices = np.random.choice(train_df1.index,replace=False)
train_df1 = train_df1.drop(drop_indices)

train_df2=train_df.loc[(train_df['label'] == df.label[2])]
drop_indices = np.random.choice(train_df2.index,replace=False)
train_df2 = train_df2.drop(drop_indices)

train_df3=train_df.loc[(train_df['label'] == df.label[3])]
drop_indices = np.random.choice(train_df3.index,replace=False)
train_df3 = train_df3.drop(drop_indices)

train_df4=train_df.loc[(train_df['label'] == df.label[4])]
drop_indices = np.random.choice(train_df4.index,replace=False)
train_df4 = train_df4.drop(drop_indices)

train_df5=train_df.loc[(train_df['label'] == df.label[5])]
drop_indices = np.random.choice(train_df5.index,replace=False)
train_df5 = train_df5.drop(drop_indices)

因此必须删除101个类别中的100张照片 我尝试删除每个数据帧的图片,但我要这样做,我应该重复100次...如何用短语法解决此问题?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)