pandas 在不舍入的情况下截断列值 [英] Pandas Truncating Column Values Without Rounding
本文介绍了 pandas 在不舍入的情况下截断列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的输入数据框;
A B
1 1.34934843904358403583408530
3 2.348430840938534058049385340983098
4 1.2309309340949084908908093947
7 15.323927236782
3 102.243984786372541562121652
我想用点后的八位数截断B列而不四舍五入,但是截断的值应该是我的新值,而不是用于显示.第一值不应存储在框架的后台.而且,B列应为数字.
I want to truncate B column with eight digits after dot without rounding, but truncated values should be my new values,not for showing. First values should not stored in the framework's background. And also, B column should be numeric.
所需的输出为;
A B
1 1.34934843
3 2.34843084
4 1.23093093
7 15.32392723
3 102.24398478
您能帮我吗?
推荐答案
想法是10**N
值的多个值,转换为int然后除以:
Idea is multiple value by 10**N
value, convert to int and then divide:
N = 8
df['B'] = df['B'].mul(10**N).astype(np.int64).div(10**N)
print (df)
A B
0 1 1.349348
1 3 2.348431
2 4 1.230931
3 7 15.323927
4 3 102.243985
另一个想法是将值精确转换为字符串,然后转换为浮点数:
Another idea is convert values to strings with precision and then to floats:
df['B'] = df['B'].map(lambda x: "{:.8f}".format(x)).astype(float)
这篇关于 pandas 在不舍入的情况下截断列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文