Truncate Decimal Places Of Values Within A Pandas Df
I can truncate individual floats using the truncate function in math. But when trying to pass the same function to a pandas df column I'm getting an error. import math import panda
Solution 1:
You can use applymap
trunc = lambda x: math.trunc(1000 * x) / 1000;
df.applymap(trunc)
Solution 2:
I believe the easiest way to achieve this would be using .astype(int)
In your example, it would be:
df[x] = ((df[x]*1000).astype(int).astype(float))/1000
Solution 3:
Try changing df['X'] = math.trunc(1000 * df['X']) / 1000;
to df['X'] =[math.trunc(1000 * val) / 1000 for val in df['X']]
. Hope it helps
Post a Comment for "Truncate Decimal Places Of Values Within A Pandas Df"