我正在使用以下代码:
cat_cols = ['MSZoning','Alley','LotShape','LandContour','Utilities','LotConfig','LandSlope','Neighborhood','Condition1','Condition2','BldgType','HouseStyle','RoofStyle','RoofMatl','Exterior1st','Exterior2nd','MasVnrType','ExterQual','ExterCond','Foundation','BsmtQual','BsmtCond','BsmtExposure','BsmtFinType1','BsmtFinType2','Heating','HeatingQC','CentralAir','Electrical','KitchenQual','Functional','FireplaceQu','GarageType','GarageFinish','GarageQual','GarageCond','PavedDrive','PoolQC','Fence','MiscFeature','SaleType','SaleCondition']
from sklearn.preprocessing import LabelEncoder
le=LabelEncoder()
for col in cat_cols:
if col in dataset_train.columns:
i = dataset_train.columns.get_loc(col)
dataset_train.iloc[:,i] =le.fit_transform(dataset_train.iloc[:,i])
它会出现以下错误:
TypeError:参数必须是字符串或数字。
list
,我就会遇到这个错误 - 例如dataset_train = pd.DataFrame({'Alley':[['A','B','C']]})
- 因此在某些列中,您可能有无法编码的值 - 您至少可以使用print(col)
来查看哪一列出现了问题。 - furas