[英]How do I fix “ValueError: could not convert string to float” when running regression from Pandas Dataframe?
I've looked through Stack Overflow and unfortunately the existing answers do not help me.我查看了 Stack Overflow,不幸的是,现有的答案对我没有帮助。
My code looks as follows:我的代码如下所示:
def regress(data1, data2, data3, data4):
df = insert_worldbankdata(data1, data2, data3, data4)
X = df['Women in Parliament (%)'], df['Inflation (%)'], df['Individuals using Internet (%)']
y = df['Annual Mean Sat.']
regr = linear_model.LinearRegression()
regr.fit(X, y)
model = sm.OLS(y, X).fit()
predictions = model.predict(X)
print_model = model.summary()
return print_model
It returns this error: ValueError: could not convert string to float:它返回此错误: ValueError:无法将字符串转换为浮点数:
Examples I have tried are:我试过的例子是:
map(float, x)
df['VARIABLE'] = df['VARIABLE'].astype(float)
As well as the replace function.以及替换 function。
I'd really appreciate any help.我真的很感激任何帮助。 I've been struggling to figure this out for the past few days and been browsing through the Stack Overflow archives.在过去的几天里,我一直在努力解决这个问题,并浏览了 Stack Overflow 档案。
Thank you!谢谢!
check the values of your columns they should be in the following format '10.5674'检查列的值,它们应采用以下格式“10.5674”
So you have to remove all other characters to avoid this error.因此,您必须删除所有其他字符以避免此错误。 Also, you have to remove all the 'nan' 'empty'... strings.此外,您必须删除所有 'nan' 'empty'... 字符串。 They will cause the same error.它们将导致相同的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.