[英]Using LabelEncorder i am trying to change the dataset from String to Numeric
I have 2 dataset train_df and test_df. 我有2个数据集train_df和test_df。 I need to change the columns ('ReqName','AreaChange', 'CodeChange','TestSuite') as Numeric.
我需要将列(“ ReqName”,“ AreaChange”,“ CodeChange”,“ TestSuite”)更改为数字。 Hence i have used LabelEncorder , while doing so, i get below error
因此,在使用LabelEncorder的同时,出现以下错误
from sklearn.preprocessing import LabelEncoder
data = [train_df, test_df]
cols = ( 'ReqName','AreaChange', 'CodeChange','TestSuite')
for c in cols:
lbl = LabelEncoder()
lbl.fit(list(data[c].values))
data[c] = lbl.transform(list(data[c].values))
I get this error: 我收到此错误:
"TypeError: list indices must be integers or slices, not str"
“ TypeError:列表索引必须是整数或切片,而不是str”
You're trying to access list element via str
objects ('ReqName','AreaChange' ...) 您正在尝试通过
str
对象('ReqName','AreaChange'...)访问列表元素
Change for
loop to this: 更改
for
循环:
for c, _ in enumerate(cols):
your error message could not be more clear : 您的错误消息无法更清晰地显示:
you are trying to access the data
list in index c
which is a string. 您正在尝试访问索引
c
的data
列表,它是一个字符串。
you must access items in list with the corresponding index - which should be integer, for example 1,2,3 etc. 您必须使用相应的索引访问列表中的项目-索引应为整数,例如1,2,3等。
so just access it with data[1]
, data[0]
etc. 因此只需使用
data[1]
, data[0]
等访问它即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.