如何摆脱数据中的加号?

时间:2019-05-01 19:46:16

标签: python pandas machine-learning scikit-learn sklearn-pandas

我有一列,其中包括stay_in_current_city_years。 还有一些数据,例如4 +

我想要一些算法。首先是线性回归

from sklearn.linear_model import LinearRegression
r=LinearRegression()
r.fit(x_train,y_train)
y_pred=r.predict(x_test)
print(y_pred)

ValueError:无法将字符串转换为float:'4 +'

1 个答案:

答案 0 :(得分:-2)

如果您要删除+,那么最容易理解的删除方法是:

str = '4+'
str = str.replace('+', '') # this will replace the '+' with ''
str = str.strip() # this will remove the leading and trailing spaces, just in case, good practice, but might not be required, voila you have just the number