How to get rid of ValueError: could not convert string to float

How to get rid of ValueError: could not convert string to float

我是运行下面的代码

reg = linear_model.LinearRegression()
reg.fit(df[['year']], df.income)

然后我收到以下错误

ValueError: could not convert string to float: '19,703,399.30'

年份值类似于 1960-2016
收入看起来像 19,703,399.30

我该如何解决这个问题?

你可以尝试以下方法吗:

df['income'] = df['income'].str.replace(',', '').astype(float)
df['year'] = df['year'].astype(float)
reg.fit(df[['year']], df.income)