You can use round:
df.lerate = df.lerate.round(2)
Example:
>>> df = pd.DataFrame(np.random.random([3, 3]),
columns=['A', 'B', 'C'], index=['first', 'second', 'third'])
>>> df.A = df.A.round(2)
>>> df
A B C
first 0.82 0.581855 0.548373
second 0.21 0.536690 0.986906
third 0.78 0.100343 0.576521
Answer from mechanical_meat on Stack Overflowpython - How do you display values in a pandas dataframe column with 2 decimal places? - 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
Round each number in a Python pandas data frame by 2 decimals - 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
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
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
import numpy as np
np.round(p_table, decimals=2)
Here is how you convert np.float64 columns to 2 decimal places
df_survey["some_column_with_too_many_decimal"] = df_survey["some_column_with_too_many_decimal"].apply(lambda x: int(x*100)/100)
Also to select only certain rows in that column if that is what you need, please use df.loc instead of iloc on every row, since the df might have too many rows.
df.loc[(df["column1"]>0), ["column2", "column3"]]
or
df.loc[(df["column1"]>0), "column2", "column3"]
The first argument to loc is a list of conditions to filter by, the second argument is the columns to select and then you can update them by using apply as show above.
If you want to use round, you can round off values then multiply by 100, convert to int and divide by 100 making it decimal with 2 places. The round function does not limit it to 2 decimal places because of the way the values are stored in the dataframe.
You can round off the DataFrame directly using
df.round(2)