Reputation: 590
I am using numpy round function its working well all aspects but when there is 15.65 it should give 15.7 but its giving 15.6. is there any other method to do this ? because i read many threads numpy have this issue.
df1['result']=df1['a'].apply(lambda x: np.round(x, decimals=1))
Upvotes: 0
Views: 850
Reputation: 1625
Use round()
python builtin function
In [1]: round(15.65, 1)
Out[1]: 15.7
In your case:
df1['result']=df1['a'].apply(lambda x: round(x, 1))
Upvotes: 0
Reputation: 349
Python inbuilt round() function rounds off to the given number of digits and returns the floating point number, if no number of digits is provided for round off , it rounds off the number to the nearest integer.
array = [15.65, 15.64, 15.61, 15, 15.94, 15.96]
print([round(i,1) for i in array])
It gives output [15.7, 15.6, 15.6, 15, 15.9, 16.0]
Upvotes: 1
Reputation: 150785
There's a trick that adds a very small amount to the data and round. But you need to assure that the delta is smaller than your resolution.
df = pd.Series(np.arange(0,100,0.05))
df.round(1).head()
# 0 0.0
# 1 0.0
# 2 0.1
# 3 0.2
# 4 0.2
# dtype: float64
(df+1e-10).round(1).head()
# 0 0.0
# 1 0.1
# 2 0.1
# 3 0.2
# 4 0.2
# dtype: float64
Upvotes: 1