Pandas Dataframe如何在不舍入的情况下截断浮点小数点? [英] Pandas Dataframe How to cut off float decimal points without rounding?

查看:168
本文介绍了Pandas Dataframe如何在不舍入的情况下截断浮点小数点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在彼此靠近的两个数据框中具有经度和纬度.如果我运行完全相似的检查,例如

I have longitude and latitude in two dataframes that are close together. If I run an exact similarity check such as

test_similar = test1_latlon.loc[~test1_latlon['cr'].isin(test2_latlon['cr'])]

我失败很多,因为很多数字在小数点后第五位.我想在第三个小数点后截断.我见过人们格式,因此它会被截断,但我想更改实际值.使用 round()会四舍五入数据,并且会出现更多错误,因此有没有办法只将小数点后3个小数点减掉?

I get a lot of failures because a lot of the numbers are off at the 5th decimal place. I want to truncate at after the 3rd decimal. I've seen people format so it shows up truncated, but I want to change the actual value. Using round() rounds off the data and I get even more errors, so is there a way to just drop after 3 decimal points?

推荐答案

如建议这里,您可以这样做:

As suggested here you can do:

x = 1.123456
float( '%.3f'%(x) )

如果您想要更多的小数位,只需将3更改为所需的任何数字即可.

if you want more decimal places, just change the 3 with any number you need.

这篇关于Pandas Dataframe如何在不舍入的情况下截断浮点小数点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆