I have a dataset:
df = pd.read_excel('/Users/Adeel/Desktop/ECON628-01-omerqureshi84/datasets/main-data.xlsx')
It has columns with names such as "lerate" which is the log of the exchange rates for countries. It's in 5 decimal places and and I'm trying to convert it to 2 decimal places.
I'm using the following code:
for i in range(len(df.lerate)):
print ("%.2f" % df.lerate[i])
It's giving me an error.
Can anyone help? I don't know what's wrong here.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With