我无法将字符串转换为float,我的变量是“ CHARGE AMOUNT IDR”。因为我想总结一下。
我已经尝试过从堆栈溢出的另一篇文章中解决问题,但这不起作用
这是我的数据类型
In [1]: df2_final.dtypes
Out[1]: CHARGE AMOUNT IDR object
我的第一个失败代码
In [2]: df2_final["ADDITIONAL CHARGES"] =df2_final["ADDITIONAL CHARGES"].astype(float)
Out[2]: TypeError: could not convert string to float: 'ADDITIONAL CHARGES '
*note: ther's blank space in the end
第二次失败代码
In [3]: df2_final["COST AMOUNT IDR"] = df2_final["COST AMOUNT IDR"].apply(lambda x: int(x.split()[0].replace(',', '')))
Out[3]: TypeError: 'int' object has no attribute 'split'
第三个失败代码
In [4]:
df2_final["ADDITIONAL CHARGES"] =df2_final["ADDITIONAL CHARGES"].astype(str)
df2_final["ADDITIONAL CHARGES"] =df2_final["ADDITIONAL CHARGES"].apply(str.strip)
df2_final["ADDITIONAL CHARGES"] =df2_final["ADDITIONAL CHARGES"].astype(float)
Out[4]: TypeError: could not convert string to float: 'ADDITIONAL CHARGES'
第四次失败代码
In [4]:
df2_final["ADDITIONAL CHARGES"] = pd.to_numeric(df2_final["ADDITIONAL CHARGES"].str.strip(), errors='coerce')
or
df2_final["ADDITIONAL CHARGES"] = pd.to_numeric(df2_final["ADDITIONAL CHARGES"], errors='coerce')
or
df2_final["ADDITIONAL CHARGES"] = df2_final["ADDITIONAL CHARGES"].convert_objects(convert_numeric=True)
Out[4]:MemoryError:
那我该怎么办?