0

I have a dataframe and I would like to round up the Loss Ratio column to 2 decimal places. Here is my dataframe:

             Month  Death benefit premium revenue  Loss Ratio  Total Claims
0        July-2020                             50       0.900         45.00
1      August-2020                             40       0.800         32.00
2   September-2020                             40       0.900         36.00
3     October-2020                             50       0.690         34.50
4    November-2020                             40       0.900         36.00
5    December-2020                             50       0.805         40.25
6     January-2021                             40       0.794         31.76
7    February-2021                             40       0.783         31.32
8       March-2021                             50       0.772         38.60
9       April-2021                             40       0.800         32.00
10        May-2021                             60       0.700         42.00
11       June-2021                             50       0.950         47.50

I would like my Loss Ratio column to become like this:

             Month  Death benefit premium revenue  Loss Ratio  Total Claims
0        July-2020                             50       0.90         45.00
1      August-2020                             40       0.80         32.00
2   September-2020                             40       0.90         36.00
3     October-2020                             50       0.70         34.50
4    November-2020                             40       0.90         36.00
5    December-2020                             50       0.81         40.25
6     January-2021                             40       0.79         31.76
7    February-2021                             40       0.78         31.32
8       March-2021                             50       0.77         38.60
9       April-2021                             40       0.80         32.00
10        May-2021                             60       0.70         42.00
11       June-2021                             50       0.95         47.50

I have tried two ways to round the Loss Ratio column to 2 decimal places using the following codes:

df['Loss Ratio'] = df['Loss Ratio'].round(2)
df['Loss Ratio'] = round(df['Loss Ratio'], 2)

My output after the running the codes is:

            Month  Death benefit premium revenue  Loss Ratio  Total Claims
0        July-2020                             50        0.90         45.00
1      August-2020                             40        0.80         32.00
2   September-2020                             40        0.90         36.00
3     October-2020                             50        0.69         34.50
4    November-2020                             40        0.90         36.00
5    December-2020                             50        0.80         40.25
6     January-2021                             40        0.79         31.76
7    February-2021                             40        0.78         31.32
8       March-2021                             50        0.77         38.60
9       April-2021                             40        0.80         32.00
10        May-2021                             60        0.70         42.00
11       June-2021                             50        0.95         47.50

I noticed that the fifth value in Loss Ratio column, 0.805 did not round to 0.81 but it rounded to 0.80 instead. Can anyone please advice me how to fix this problem please? Thank you so much for your help!

newt335
  • 175
  • 1
  • 1
  • 9
  • 1
    This is intentional; as I understand it pandas uses 'half rounding' or 'bankers rounding' by default; there are good reasons to do so (if you always round up, you are biasing your total and average upward). – Halbert Dec 05 '21 at 06:00
  • 1
    Separately, it might be that your data is actually rounded in the first place (and due to how floating point numbers work, it certainly *is* rounded), and the true value is for instance .804999997 -> which rounds to .805 with three significant digits, but 0.80 with 2. – Halbert Dec 05 '21 at 06:03
  • 1
    See duplicate. In summary `0.805` -> `0.80` ; `0.815` -> `0.82` – mozway Dec 05 '21 at 06:04

0 Answers0