Pandas Dataframe How to cut off float decimal points without rounding?

若如初见. 提交于 2020-01-30 06:32:17

问题


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'])]

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?


回答1:


As suggested here you can do:

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

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




回答2:


import math

value1 = 1.1236
value2 = 1.1266

value1 = math.trunc(1000 * value1) / 1000;
value2 = math.trunc(1000 * value2) / 1000;

#value1 output
1.123

#value2 output
1.126


来源:https://stackoverflow.com/questions/56780561/pandas-dataframe-how-to-cut-off-float-decimal-points-without-rounding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!