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))
Method 1: Using Built-in round() Function. In Python there is a built-in round() function which rounds off a number to the given number of digits. The function round() accepts two numeric arguments, n and n digits and then returns the number n after rounding it to n digits.
Use the round() function to round a number to 1 decimal, e.g. result = round(2.5678, 1) . The round() function will return the number rounded to 1 digit precision after the decimal point.
Python round() Function The round() function returns a floating point number that is a rounded version of the specified number, with the specified number of decimals. The default number of decimals is 0, meaning that the function will return the nearest integer.
Python has an inbuilt function round(), which will take the float value and round it off. The round() function takes two parameters: Number and Ndigits (optional). Ndigits represent the number of decimal places to round.
Your numbers are rounded correctly. Below I will explain,
If you really want just to show two digits, you can skip the rounding function (custom_round
) altogether, and just run this* before printing your dataframes:
pd.options.display.float_format = '{:,.2f}'.format
This will make the float valued data to be printed with 2 digits precision. Example:
table=pd.DataFrame({'A': [0.62435, 0.542345,0.213452],
'B': [0.22426,0.18779,0.30346]})
In [1]: table
Out[1]:
A B
0 0.62 0.22
1 0.54 0.19
2 0.21 0.30
table=pd.DataFrame({'A': [0.62435, 0.542345,0.213452],
'B': [0.22426,0.15779,0.30346]})
# execute code with custom_round in the question
In [1]: table
Out[1]:
A B
0 0.60 0.20
1 0.55 0.15
2 0.20 0.30
table=pd.DataFrame({'A': [0.62435, 0.542345,0.213452],
'B': [0.22426,0.18779,0.30346]})
# execute code with custom_round in the question
In [1]: table
Out[1]:
A B
0 0.60 0.2
1 0.55 0.2
2 0.20 0.3
Internally, the number is rounded into two digit precision. When you print the table to the console / Jupyter notebook, pandas skips printing of the last value (2nd digit) if they are all zeroes. So, the data is two digits precision (for example, 0.20), but it is just shown with one digit precision, since 0.20 = 0.2.
* You may also use other printing scheme: The pd.options.display.float_format
can be set to any callable that
[...] accept a floating point number and return a string with the desired format of the number. This is used in some places like SeriesFormatter. See core.format.EngFormatter for an example.
In your second screenshot the second value in column B is 0.22 which is correctly rounded then to 0.2. All values in the second screenshot round to 0.x0. So the missing last digit is a feature from the GUI, suppressing a trailing 0.
The error is likely not in the rounding to 0.05. It is before that.
It appears as if the rounding to two digits using round(2) is not applied to the input in your example (the second value in B in your example is 0.15779.
Pandas has this thing which removes trailing zeros for digits after trailing zeros. I guess its sort of a feature or a bug. If you just want to see the output to the right precision on your display/print, have you tried the display_precison option, like
pd.set_option('precision', 2)
Or change 2 to 3 or 4 to play around. I think this is globally display precision option though, so if you want to display different precision for different column, that will be a problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With