Pandas条件修改csv文件记录

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 15:48   3781   0

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb3 in position 0: invalid start byte

注意两点:

encoding='gb18030' # 可以解决上述报错;

df.loc[] # 可以做条件修改,并新增列;

import cv2
import os
import pandas as pd
import numpy as np
from path import DATA_PATH
batch_size = 1
DATA_ID = 'BeijingGarbage'
NB_CLASSES = 4

# 读取数据
df = pd.read_csv(os.path.join(DATA_PATH, DATA_ID, 'train.csv'),encoding='gb18030')
# 将name列含有"可回收物"字符串,新增一列new_label,值为0
# loc[df['name'].str[0:4] #截取前4位字符串进行匹配,做条件修改
df.loc[df['name'].str[0:4]=='可回收物','new_label'] = '0'
df.loc[df['name'].str[0:4]=='厨余垃圾','new_label'] = '1'
df.loc[df['name'].str[0:4]=='有害垃圾','new_label'] = '2'
df.loc[df['name'].str[0:4]=='其他垃圾','new_label'] = '3'
image_path_list = df['image_path'].values
label_list = df['label'].values
print(df)

batch_x = []
batch_y = []
while len(batch_x) < batch_size:
    index = np.random.randint(len(image_path_list))
    img = cv2.imread(os.path.join(DATA_PATH, DATA_ID, image_path_list[index]))
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    label = np.zeros(NB_CLASSES)
    label[label_list[index]] = 1
    print(label)
    img = cv2.resize(img, (224, 224))
    batch_x.append(img)
    batch_y.append(label)
    cv2.imshow('frame', img)
    cv2.waitKey()

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP