Converting my column to 2 decimal places

12,345

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
Share:
12,345
Omer Qureshi
Author by

Omer Qureshi

Updated on June 17, 2022

Comments

  • Omer Qureshi
    Omer Qureshi almost 2 years

    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.