Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting my column to 2 decimal places

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.

like image 892
Omer Qureshi Avatar asked Jan 05 '23 11:01

Omer Qureshi


1 Answers

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
like image 187
mechanical_meat Avatar answered Jan 13 '23 13:01

mechanical_meat