简体   繁体   English

从 Pandas Dataframe 运行回归时,如何修复“ValueError:无法将字符串转换为浮点数”?

[英]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.

相关问题 ValueError:无法将字符串转换为浮点数:'' Pandas DataFrame - ValueError: could not convert string to float: '' Pandas DataFrame 如何修复“ValueError:无法将字符串转换为浮点数”? - How to fix “ValueError: could not convert string to float”? 如何修复“ValueError:无法将字符串转换为浮点数” - How to fix “ValueError: could not convert string to float” ValueError:使用 astype 时无法在 pandas 中将字符串转换为浮点数 - ValueError: could not convert string to float in pandas when using astype 如何修复 ValueError :无法将字符串转换为浮点数:在 Python 中 - how to fix ValueError :could not convert string to float: in Python 如何修复“ValueError:无法将字符串转换为浮点数:'East'”(Python) - How to fix "ValueError: could not convert string to float: 'East'" (Python) 如何使用tkinter修复Python中的“ ValueError:无法将字符串转换为float:” - How to fix “ValueError: could not convert string to float:” in Python with tkinter 如何修复此错误:ValueError:无法将字符串转换为浮点数:'A' - How to fix this error: ValueError: could not convert string to float: 'A' 如何修复 ValueError: could not convert string to float: in python - How to fix the ValueError: could not convert string to float: in python ValueError:无法将字符串转换为浮点数 - kmeans(熊猫) - ValueError: could not convert string to float - kmeans (pandas)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM