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