相关疑难解决方法(0)

在Python中截断为三位小数

我如何获得1324343032.324?

如下所示,以下内容不起作用:

>>1324343032.324325235 * 1000 / 1000
1324343032.3243253
>>int(1324343032.324325235 * 1000) / 1000.0
1324343032.3239999
>>round(int(1324343032.324325235 * 1000) / 1000.0,3)
1324343032.3239999
>>str(1324343032.3239999)
'1324343032.32'
Run Code Online (Sandbox Code Playgroud)

python

46
推荐指数
9
解决办法
11万
查看次数

截断pandas df中值的小数位

我可以truncate使用单独的花车truncate在功能上math。但是当试图将相同的函数传递给一pandas df列时,我收到了一个错误。

import math
import pandas as pd

X = 1.1236

X = math.trunc(1000 * X) / 1000;

#Output
1.123
Run Code Online (Sandbox Code Playgroud)

但是当使用 a 时pandas df

d = ({
    'X' : [1.1234,1.1235],           
    })

df = pd.DataFrame(data=d)

df['X'] = math.trunc(1000 * df['X']) / 1000;
Run Code Online (Sandbox Code Playgroud)

错误:

df['X'] = math.trunc(1000 * df['X']) / 1000;

TypeError: type Series doesn't define __trunc__ method
Run Code Online (Sandbox Code Playgroud)

python math truncate pandas

2
推荐指数
2
解决办法
3960
查看次数

标签 统计

python ×2

math ×1

pandas ×1

truncate ×1