[英]Iterating through columns, not iterable
What causes the code below to give the following error on line 8:是什么导致下面的代码在第 8 行出现以下错误:
Type error: 'Float' object is not iterable
类型错误:'Float' object 不可迭代
for column in usable_columns:
cardinality = len(np.unique(x_train[column]))
if cardinality == 1:
x_train.drop(column, axis=1) # Column with only one
# value is useless so we drop it
x_test.drop(column, axis=1)
if cardinality > 2: # Column is categorical
mapper = lambda x: sum([ord(digit) for digit in x])
x_train[column] = x_train[column].apply(mapper)
x_test[column] = x_test[column].apply(mapper)
x_train.head()
A for loop can only function if the number of times it iterates is a whole number.一个for循环如果迭代次数是整数的话只能是function。 whatever the variable
column
is, its being stored as a float not an integer (ie 3.0 instead of 3) Assuming that column
is a whole number, either use column=int(column)
before the for loop or type for int(column) in usable_columns
as your for loop无论变量
column
是什么,它都存储为浮点数而不是 integer(即 3.0 而不是 3)假设该column
是一个整数,在 for 循环之前使用column=int(column)
或for int(column) in usable_columns
作为你的 for 循环
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.