简体   繁体   English

如何修复 Python 中的“无法将字符串转换为浮点数”?

[英]How do you fix “could not convert string to float” in Python?

To simplify, I have two columns in a dataframe, df, in Python Jupyter Notebook:为简化起见,我在 Python Jupyter Notebook 的 dataframe、df 中有两列:

Column            Non-Null Count  Dtype  

Cash              57 non-null     object 
FX Rate           57 non-null     float64

I have tried to convert column 'Cash' to float as it may be a string.我试图将列“现金”转换为浮动,因为它可能是一个字符串。 I then wish to create a new column that is simple multiplication of two columns, such as:然后我希望创建一个新列,它是两列的简单乘法,例如:

df["GBP Market Value"] = df["Cash"] * df["FX Rate"]

However I get the error:但是我得到了错误:

TypeError: can't multiply sequence by non-int of type 'float' TypeError:不能将序列乘以“float”类型的非整数

df was created by pandas read_csv. df 由 pandas read_csv 创建。 If I open the data in Excel, I can do column multiplication so I'm thinking even if the underlying data in that 'Cash' column is a string, how can format the column to do the calculation in Python.如果我打开 Excel 中的数据,我可以进行列乘法,所以我在想即使“现金”列中的基础数据是一个字符串,如何格式化该列以在 Python 中进行计算。 Thanks谢谢

df["Cash"] = pd.to_numeric(df["Cash"],errors='coerce') df["Cash"] = pd.to_numeric(df["Cash"],errors='coerce')

Worked工作过

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复 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: could not convert string to float: in python - How to fix the ValueError: could not convert string to float: in python 如何修复“ValueError:无法将字符串转换为浮点数” - How to fix “ValueError: could not convert string to float” 如何修复“ValueError:无法将字符串转换为浮点数”? - How to fix “ValueError: could not convert string to float”? 从 Pandas Dataframe 运行回归时,如何修复“ValueError:无法将字符串转换为浮点数”? - How do I fix “ValueError: could not convert string to float” when running regression from Pandas Dataframe? 如何修复错误 ValueError: could not convert string to float in a NLP project in python? - how to fix the error ValueError: could not convert string to float in a NLP project in python? 如何解决此错误:CSV文件无法将字符串转换为浮点错误 - How to fix this error: Could not convert string to float error with csv file 如何修复此错误:ValueError:无法将字符串转换为浮点数:'A' - How to fix this error: ValueError: could not convert string to float: 'A'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM