user8566323
user8566323

Reputation:

Convert values in pandas dataframe to two decimal points

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?

Upvotes: 3

Views: 18702

Answers (2)

Enrico Gandini
Enrico Gandini

Reputation: 1015

The round method only works as I think you want if the values in each column (i.e., in each pandas.Series) of the DataFrame already have more decimal points than the value you are passing to round.

For instance:

pd.Series([1.09185, 2.31476]).round(2)

returns:

0    1.09
1    2.31
dtype: float64

But if the Series has fewer decimal points than the number you are trying to round, you will not get the desired visual result. For instance:

pd.Series([1.6, 2.3]).round(2)

returns:

0    1.6
1    2.3
dtype: float64

This is mathematically correct, since the numbers in the second Series already have fewer decimal points than 2. But it is not what you visually expect.

If you only want to change the display of a Series or DataFrame inside a notebook, you should use pandas.set_option("display.precision", 2). This changes the visual representation of the Series or DataFrame, without changing the inner precision of the actual numbers.

If for some reason you need to save a Series or DataFrame with the numbers already with the desired decimal points, you can apply a function that converts the object to string type and formats the string:

pd.Series([1.6, 2.3]).apply(lambda x: f"{x:.2f}")

which returns a new Series of dtype object instead of float:

0    1.60
1    2.30
dtype: object

Upvotes: 2

jezrael
jezrael

Reputation: 862521

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

Upvotes: 15

Related Questions