如何将我的数据框转换为完整的 int 数据框?

How to convert my dataframe to a complete int dataframe?

我需要 运行 二叉树分类器在给我的数据集上。

这就是我所做的。

data = pd.read_csv('cars.csv')
print(data.head())
x = data.drop(' brand', axis = 1)
y = data[' brand']
    mpg   cylinders  cubicinches   hp  weightlbs   time-to-60   year     brand
0  14.0           8          350  165       4209           12   1972       US.
1  31.9           4           89   71       1925           14   1980   Europe.
2  17.0           8          302  140       3449           11   1971       US.
3  15.0           8          400  150       3761           10   1971       US.
4  30.5           4           98   63       2051           17   1978       US.
x = x.values
print(x)
[[14.0 8 '350' ... '4209' 12 1972]
 [31.9 4 '89' ... '1925' 14 1980]
 [17.0 8 '302' ... '3449' 11 1971]
 ...
 [22.0 6 '232' ... '2835' 15 1983]
 [18.0 6 '232' ... '3288' 16 1972]
 [22.0 6 '250' ... '3353' 15 1977]]

当我 运行 fit() 函数时,出现错误

ValueError: could not convert string to float:

所以,我猜这是因为我在引号中得到的值。 无论如何,如果你明白为什么我在引号中得到一些价值,请告诉我。 (cars.csv 是一个普通的 csv 文件,没有什么特别之处,所以 idk)

您可以像这样将 CSV 文件中存储为字符串的列转换为浮点数:

df['columnname'].apply(lambda x:float(x))