Home > Back-end >  removing 0 after decimal 4th digit
removing 0 after decimal 4th digit

Time:12-22

I have an excel file with coordinates. I want to take only the first 3 digits if the fourth digit is 0 after the decimal.

Original data    Output required
12.6820701       12.682
-0.58903705      -0.589
56.345698        56.345698
8.95604          8.956
21.99911         21.99911

CodePudding user response:

Try this:

df['Output required'] = df['Original data'].astype(str).apply(lambda v: round(float(v), 3) if ('.' in v and len(v) >= v.find('.') 4 and v[v.find('.') 4] == '0') else float(v))

Output:

>>> df
Original data    Output required
12.6820701       12.682
-0.58903705      -0.589
56.345698        56.345698
8.95604          8.956
21.99911         21.99911

CodePudding user response:

import numpy as np
df['Output required'] = np.float("{:.4f}".format(df['Original data']))
  • Related