BusterTheFatCat
BusterTheFatCat

Reputation: 57

How to change each individual value of column

In my dataframe, I have two columns for latitude and longintude that are currently of type float. all of its values are six digits after the decimal, so the first value of the column would be like 45.392503 and the next would be 45.309583, etc all the way to the end.

I would like each value in these two columns to be truncated to 3 digits after the decimal.

What I've tried:

# for i, j in zip(df['LAT_CTR'], df['LONG_CTR']):
#     i = '%.3f' % (i)
#     j = '%.3f' % (j)

OR

# for i in range(0, len(df['LONG_CTR'])):
#     df['LONG_CTR'][i] = '%.3f' % (i)

and I was thinking to just convert the entire column using .astype('float64') but can't seem to get that far

So I am reading through some other answers, one suggestion that might apply to mine is to create a function that does this, using list comprehension, something like [f(x) for i in df['LAT_CTR']] could this work? Is there a faster way to apply these changes?

Upvotes: 0

Views: 29

Answers (1)

Anna Nevison
Anna Nevison

Reputation: 2759

Just use the built in round function

df['LONG_CTR'] = [round(x, 3) for x in df['LONG_CTR'].tolist()]

Upvotes: 1

Related Questions