[英]Remove letters from my numeric columns doesn't work
I have a x_train
like this (all the columns are object
type):我有一个这样的
x_train
(所有列都是object
类型):
a b c
1 2 523f
2 45 52A
3 32 95
4 245 84A
5 86 42
6 7 52
7 45 31
7a 45 712
8b 53 62
194v 34 3
The Y_train
only have 0 and 1. I tried to use RF.fit(x_train, Y_train)
but I got an error: Y_train
只有 0 和 1。我尝试使用RF.fit(x_train, Y_train)
但出现错误:
could not convert string to float: 7a
I try to have only the numeric value and remove the letters, so I tried to use something like:我尝试只有数值并删除字母,所以我尝试使用类似的东西:
x_train = re.findall(r'\d+\d+', x['a'])
but it doesn't work.但它不起作用。 How can I fix this?
我怎样才能解决这个问题?
Assuming all integers, you can use this for any column that has non-numeric values:假设所有整数,您可以将其用于任何具有非数字值的列:
df[col] = df[col].str.replace('\D', '', regex=True).astype(int)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.