Convert values in pandas dataframe to two decimal points

14,604

It seems you need DataFrame.round:

df = df.round(2)
print (df)
      NO  Topic A  Topic B  Topic C
0    0.0     1.00     1.00     1.00
1    1.0     0.55     0.64     0.55
2    2.0     0.57     0.74     0.68
3    3.0     0.85     0.86     0.85
4    4.0     0.20     0.20     0.20
5    5.0     0.85     0.84     0.85
6    6.0     0.45     0.53     0.45
7    7.0     0.62     0.66     0.70
8    8.0     0.57     0.50     0.57
9    9.0     0.85     0.90     0.88
10  10.0     0.95     0.97     0.96
Share:
14,604

Related videos on Youtube

Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    I have a pandas dataframe as follows.

         NO                Topic A                   Topic B            Topic C
    0    0.0                1.000                      1.000            1.000   
    1    1.0                0.550                      0.640            0.550   
    2    2.0                0.567                      0.740            0.675   
    3    3.0                0.850                      0.860            0.850   
    4    4.0                0.200                      0.200            0.200   
    5    5.0                0.850                      0.840            0.850   
    6    6.0                0.450                      0.533            0.450   
    7    7.0                0.625                      0.657            0.700   
    8    8.0                0.575                      0.500            0.575   
    9    9.0                0.850                      0.900            0.880   
    10  10.0                0.950                      0.971            0.960
    

    Now I want to update the entire dataframe in a way it contains values of two decimal ponits. e.g., 0.625 -> 0.63 etc.

    Is it possible to do it in pandas?

  • jezrael
    jezrael over 6 years
    Glad can help! Nice day!
  • cph_sto
    cph_sto about 4 years
    What if you want the column NO to have 2 decimals places instead of 1? Like 0.00 instead of 0.0
  • jezrael
    jezrael about 4 years
    @cph_sto - then convert it to strings with formating.
  • cph_sto
    cph_sto about 4 years
    Oh ok. Thanks for quick reply.