Omer Qureshi
Omer Qureshi

Reputation: 59

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.

Upvotes: 4

Views: 25391

Answers (1)

mechanical_meat
mechanical_meat

Reputation: 169284

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

Upvotes: 11

Related Questions