Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

limiting the number of decimal places in python pandas table

I was trying to rewrite a CSV file using pandas module in python. I tried to multiply the first column (excluding the title) by 60 as below,

 f=001.csv
 Urbs_Data=pd.read_csv(f,header=None)
 Urbs_Data=Urbs_Data.replace("Time_hrs","Time_min")
 Urbs_Data.loc[1:,0]=Urbs_Data.loc[1:,0].astype(float)
 Urbs_Data.loc[1:,0]*=60

It gives me some funny number for the first column, as

124.98000000000002,462.67

130.01999999999998,460.34

135.0,454.36

139.98000000000002,443.29

Is there any way to limit the number of decimal places for those numbers (to 2)? I tried to use the normal round function, it does not work for me.

like image 383
Zhifang Hu Avatar asked Feb 04 '19 00:02

Zhifang Hu


1 Answers

The DataFrame round method should work...

import numpy as np
import pandas as pd 

some_numbers = np.random.ranf(5)

df = pd.DataFrame({'random_numbers':some_numbers})

rounded_df = df.round(decimals=2)
like image 174
FChm Avatar answered Oct 03 '22 14:10

FChm