Reputation: 555
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?
Upvotes: 6
Views: 18251
Reputation: 2644
You may want to use numpy.trunc:
import numpy as np
import pandas as pd
df = pd.DataFrame([[1.2366, 1.2310], [1, 1]])
df1 = np.trunc(1000 * df) / 1000
print(df1, type(df1))
# 0 1
# 0 1.236 1.231
# 1 1.000 1.000 <class 'pandas.core.frame.DataFrame'>
Note that df1 is still a DataFrame not a numpy.array
Upvotes: 10
Reputation: 368
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
Upvotes: 4