Home > Enterprise >  How to cut float value to 2 decimal points
How to cut float value to 2 decimal points

Time:06-18

I have a Pandas Dataframe with a float column. The values in that column have many decimal points but I only need 2 decimal points. I don't want to round, but truncate the value after the second digit.

this is what I have so far, however with this operation i always get NaN's:

t['latitude']=[18.398, 18.4439, 18.346, 37.5079, 38.11, 38.2927]
sub = "."
t['latitude'].astype(str).str.slice(start=t['latitude'].astype(str).str.find(sub), stop=t['latitude'].astype(str).str.find(sub) 2)

Output:

0   NaN
1   NaN
2   NaN
3   NaN
4   NaN
5   NaN
Name: latitude, dtype: float64

CodePudding user response:

Use numpy.trunc for a vectorial operation:

n = 2 # number of decimals to keep
np.trunc(df['latitude'].mul(10**n)).div(10**n)

# to assign
# df['latitude'] = np.trunc(df['latitude'].mul(10**n)).div(10**n)

output:

0    18.39
1    18.44
2    18.34
3    37.50
4    38.11
5    38.29
Name: latitude, dtype: float64

CodePudding user response:

The simpliest way to truncate:

t = pd.DataFrame()
t['latitude']=[18.398, 18.4439, 18.346, 37.5079, 38.11, 38.2927]
t['latitude'] = (t['latitude'] * 100).astype(int) / 100
print(t)
>>
latitude
0     18.39
1     18.44
2     18.34
3     37.50
4     38.11
5     38.29

CodePudding user response:

Use np.round -

s = pd.Series([18.3988, 18.4439, 18.3467, 37.5079, 38.1102, 38.2927])
s_rounded = np.round(s, 2)

Output

0    18.40
1    18.44
2    18.35
3    37.51
4    38.11
5    38.29
dtype: float64

If you don't want to round, but just truncate -

s.astype(str).str.split('.').apply(lambda x: str(x[0])   '.'   str(x[1])[:2])

Output

0    18.39
1    18.44
2    18.34
3    37.50
4    38.11
5    38.29
dtype: object

CodePudding user response:

Try

import math

for i in t[‘latitude’]:
    math.trunc(i)

CodePudding user response:

x = 12.3614
y = round(x,2)
print(y) // 12.36

CodePudding user response:

Easiest is Serious.round, but you can also try .str.extract

t['latitude'] = (t['latitude'].astype(str)
                 .str.extract('(.*\.\d{0,2})')
                 .astype(float))
print(t)

   latitude
0     18.39
1     18.44
2     18.34
3     37.50
4     38.11
5     38.29

CodePudding user response:

import re

t = [18.398, 18.4439, 18.346, 37.5079, 38.11, 38.2927]
truncated_lat=[]

for lat in t:
    truncated_lat.append(float(re.findall('[0-9] \.[0-9]{2}', str(lat))[0]))
print(truncated_lat)

Output:

[18.39, 18.44, 18.34, 37.5, 38.11, 38.29]
  • Related