准备
import pandas as pd
tmp_file = pd.read_csv('.../test_for_map.csv')
原来的做法:
for i in range(len(tmp_file['类别'])):
if tmp_file['类别'][i] == '水杯':
tmp_file['类别'][i] = 1
else:
tmp_file['类别'][i] = 0
缺点:
-
慢
-
出现warning:
SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame
使用map:
tmp_file = tmp_file.rename(columns={'类别': 'Category'})
train_resample['Category'] = train_resample.Category.map(lambda x: 1 if x =='水杯' else 0)