【发布时间】:2019-06-27 00:12:23
【问题描述】:
我可以使用math 中的truncate 函数truncate 单独浮动。但是,当尝试将相同的函数传递给 pandas df 列时,我遇到了错误。
import math
import pandas as pd
X = 1.1236
X = math.trunc(1000 * X) / 1000;
#Output
1.123
但是当使用pandas df:
d = ({
'X' : [1.1234,1.1235],
})
df = pd.DataFrame(data=d)
df['X'] = math.trunc(1000 * df['X']) / 1000;
错误:
df['X'] = math.trunc(1000 * df['X']) / 1000;
TypeError: type Series doesn't define __trunc__ method
【问题讨论】:
-
为什么要使用 truncate 而不是 df.round?
-
与其他具有 n 个小数位的数字进行比较。 11.23亿和11.24亿相差很大
标签: python pandas math truncate