Skip to main content
 首页 » 编程设计

python之Pandas之将字符串类型转换为 float

2025年02月15日26zhoujg

我在 DF 中有以下列。我如何将此列转换为具有 5 个小数位的 float(13,5)。字符串长度始终为 18 个字符。作为解决方法,我使用了字符串拆分函数并连接了返回值。

df=pd.DataFrame(['+00000030454360000','-00000030734250000','-00000004643685000'],columns=['qty']) 
print df.qty.str[:13]+'.'+df.qty.str[13:] 

预期输出

                    0 
0  +304543.6 
1  -307342.5 
2  -46436.85 

请您参考如下方法:

试试这个:

In [23]: pd.to_numeric(df.qty, errors='coerce') / 10**5 
Out[23]: 
0    304543.60 
1   -307342.50 
2    -46436.85 
Name: qty, dtype: float64