如何将对象转换为在熊猫中漂浮

时间:2020-05-27 18:46:55

标签: python pandas

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

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

如何将字符串转换为仅在列中的大熊猫中漂浮?

2 个答案:

答案 0 :(得分:0)

考虑:

import pandas as 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

      col    col2
0     4,2  4.2000
1    5.79  5.7900
2  -6,23% -0.0623
3    9.6%  0.0960

答案 1 :(得分:0)

您可以在下面使用类似的内容:-

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