gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 05:14:42 24 4
gpt4 key购买 nike

我在 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

关于python - Pandas - 将字符串类型转换为 float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42074308/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com