Python 如何将对象转换为浮动对象

Python 如何将对象转换为浮动对象,python,pandas,Python,Pandas,如何将字符串转换为只在列中浮动?考虑: 将熊猫作为pd导入 df=pd.DataFrame({“col”:[“4,2”,“5.79”,“6,23%”,“9.6%”]) df[“col2”]=df[“col”].str.replace(“,”,“).str.replace(“%”,“*0.01”).map(eval) >>>df col2 0 4,2 4.2000 1 5.79 5.7900 2 -6,23% -0.0623 3 9.6% 0.0960 您可以使用以下类

如何将字符串转换为只在列中浮动?

考虑:

将熊猫作为pd导入
df=pd.DataFrame({“col”:[“4,2”,“5.79”,“6,23%”,“9.6%”])
df[“col2”]=df[“col”].str.replace(“,”,“).str.replace(“%”,“*0.01”).map(eval)
>>>df
col2
0     4,2  4.2000
1    5.79  5.7900
2  -6,23% -0.0623
3    9.6%  0.0960

您可以使用以下类似的方法:-

base = base["DY"].str.replace(',', '.').astype(float) 

ValueError: could not convert string to float: '6.1%'

您的字符串似乎包含“%”符号。请显示一些示例输入。您希望将
6.1%
转换为
0.061
,或者更确切地说是
6.1
?我这样做:base['DY']=base['DY'].str.rstrip('%')。str.replace(',“)。astype('float')/100.0结果值错误:无法将字符串转换为float:'46.489.4'是值46489.6?
base = base["DY"].str.replace(',' , '.').replace('%', '').astype(float)