I have pandas table with two columns with numerical data (dtype flaot64). I have rounded each column to have 2 digits after the decimal point and then used function to round it to the near 0.5 but for some reason only one column got rounded with 0.05 and the second one got rounded but missed the 2nd digit.
This is fake example which works and show the flow :
table=pd.DataFrame({'A': [0.62435, 0.542345,0.213452],
'B': [0.22426,0.15779,0.30346]})
#function for round to near 0.5:
def custom_round(x, base=5):
return base * round(float(x)/base)
table['A'] = table['A'].astype(float).round(2).apply(lambda x: custom_round(x, base=.05))
table['B'] = table['B'].astype(float).round(2).apply(lambda x: custom_round(x, base=.05))
table
>>>
A B
0 0.60 0.20
1 0.55 0.15
2 0.20 0.30
but on my table I get in the end:
When I run the script without the function to round near 0.5, I still get the two digits:
table['B'] = table['B'].round(2)
My question is why is this hapenning? and how can I fix it in order to round both columns to 0.05 and get both digits appear?
edit: I have been asked how do I apply it on my real table , so:
df['A'] = df['A'].astype(float).round(2).apply(lambda x: custom_round(x, base=.05))
df['B']= df['B'].round(2).apply(lambda x: custom_round(x, base=.05))