Since 0.17.0 version you can do .round(n)
df.round(2)
0 1 2 3
0 0.06 0.67 0.77 0.71
1 0.80 0.56 0.97 0.15
2 0.03 0.59 0.11 0.95
3 0.33 0.19 0.46 0.92
df
0 1 2 3
0 0.057116 0.669422 0.767117 0.708115
1 0.796867 0.557761 0.965837 0.147157
2 0.029647 0.593893 0.114066 0.950810
3 0.325707 0.193619 0.457812 0.920403
Answer from piroot on Stack OverflowSince 0.17.0 version you can do .round(n)
df.round(2)
0 1 2 3
0 0.06 0.67 0.77 0.71
1 0.80 0.56 0.97 0.15
2 0.03 0.59 0.11 0.95
3 0.33 0.19 0.46 0.92
df
0 1 2 3
0 0.057116 0.669422 0.767117 0.708115
1 0.796867 0.557761 0.965837 0.147157
2 0.029647 0.593893 0.114066 0.950810
3 0.325707 0.193619 0.457812 0.920403
import numpy as np
np.round(p_table, decimals=2)
python - How do you display values in a pandas dataframe column with 2 decimal places? - Stack Overflow
python - Display 2 decimal places, and use commas to separate thousands, in Jupyter/pandas? - Stack Overflow
How to format float values to 2 decimal place in a dataframe except one column of the dataframe
Convert values in pandas dataframe to two decimal points - Stack Overflow
If you want to only modify the format of your values without doing any operation in pandas, you should just execute the following instruction:
pd.options.display.float_format = "{:,.2f}".format
This forces it not to use scientific notation (exponential notation) and always displays 2 places after the decimal point. It also adds commas.
You should be able to get more info here:
https://pandas.pydata.org/docs/user_guide/options.html#number-formatting
Examples:
0.0012 0.00
0.0123 0.01
1.2345 1.23
12.345 12.35
100 100.00
1234567890.123456 1,234,567,890.12
Try:
import pandas as pd
pd.set_option('display.precision', 2)
This causes it to use scientific (exponential) notation when appropriate, and keeps 2 decimal places. It makes the decision about whether to use scientific notation or not on a per-column basis, so if 1 value requires scientific notation, the whole column is displayed that way.
Examples:
0.0012 1.23e-03
0.0123 1.23e-02
100 1.00e+02
1234567890.123456 1.23e+09
Configure the following option in any cell:
pandas.options.display.float_format = '{:,.2f}'.format
You can also format the output for any float throughout the notebook with this magic command:
%precision %.2f
You can set the display.precision option with e.g.:
pd.set_option('precision', 7)
when you are done, reset it back using
pd.reset_option('precision')
as documented in https://pandas.pydata.org/pandas-docs/stable/user_guide/options.html
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
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
You forgot to make a string:
format_percent = '{:.2f}'.format(percent_val)
# ^ ^
Also, if you want a percent, you'll need to multiply by 100, and if you're on Python 2 (I can't tell), you'll either need to use floats or from __future__ import division.
If you want to round the number to two decimal places, rather than creating formatted output, there's the round function:
rounded = round(percent_val, 2)
Then your output will be a float instead of a string, and you can keep doing math with it.
You can plug the display format into pandas' display options:
In [11]: df = pd.DataFrame(np.random.randn(2, 2))
In [12]: df
Out[12]:
0 1
0 1.058814 -0.011675
1 -0.002627 -0.152505
In [13]: pd.options.display.float_format = '{:.2f}'.format
In [14]: df
Out[14]:
0 1
0 1.06 -0.01
1 -0.00 -0.15
See more about python's string formatting here.
Note: the numbers themselves are unaffected (they haven't been rounded):
In [15]: df.iloc[0, 0]
Out[15]: 1.058814403984879
In case someone wants a quick way to apply the same precision to all numeric types in the dataframe (while not worrying about str types):
pd.set_option('display.precision', 2)
This works for displaying DataFrame and Styler objects in jupyter notebooks.
In [188]: df
Out[188]:
a b c
0 1.0000 2.2460 2.0000
1 3.0000 4.4920 6.0000
2 5.0000 6.7380 10.0000
In [189]: pd.options.display.float_format = '{:,.2f}'.format
In [190]: df.apply(lambda x: x.astype(int) if np.allclose(x, x.astype(int)) else x)
Out[190]:
a b c
0 1 2.25 2
1 3 4.49 6
2 5 6.74 10
UPDATE:
In [222]: df
Out[222]:
0 1
0 3.0000 5.6000
1 1.2000 3.4560
In [223]: df.applymap(lambda x: str(int(x)) if abs(x - int(x)) < 1e-6 else str(round(x,2)))
Out[223]:
0 1
0 3 5.6
1 1.2 3.46
NOTE: be aware that .applymap() method is pretty slow as it's doing map(func, series) for each series in the DataFrame