Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas round is not working for DataFrame

Round works on a single element but not the DataFrame, tried DataFrame.round() but didn't work... any idea? Thanks.

Have code below:

print "Panda Version: ", pd.__version__
print "['5am'][0]: ", x3['5am'][0]
print "Round element: ", np.round(x3['5am'][0]*4) /4
print "Round Dataframe: \r\n", np.round(x3 * 4, decimals=2) / 4
df = np.round(x3 * 4, decimals=2) / 4
print "Round Dataframe Again: \r\n", df.round(2)

Got result:

Panda Version:  0.18.0
['5am'][0]:  0.279914529915
Round element:  0.25
Round Dataframe:
                 5am       6am      7am      8am      9am     10am     11am
Date
2016-07-11  0.279915  0.279915  2.85256  4.52778  6.23291  9.01496  8.53632
2016-07-12  0.339744  0.369658  2.67308  4.52778  5.00641  7.30983  6.98077
2016-07-13  0.399573  0.459402  2.61325  3.83974  5.48504  6.77137  5.24573
2016-07-14  0.339744  0.549145  2.64316  3.36111  5.66453  5.96368  7.87821
2016-07-15  0.309829  0.459402  2.55342  4.64744  4.46795  6.80128  6.17308
2016-07-16      0.25  0.369658  2.46368  2.67308  4.58761  6.35256  5.63462
2016-07-17  0.279915  0.369658  2.58333  2.91239  4.19872  5.51496  6.65171
Round Dataframe Again:
                 5am       6am      7am      8am      9am     10am     11am
Date
2016-07-11  0.279915  0.279915  2.85256  4.52778  6.23291  9.01496  8.53632
2016-07-12  0.339744  0.369658  2.67308  4.52778  5.00641  7.30983  6.98077
2016-07-13  0.399573  0.459402  2.61325  3.83974  5.48504  6.77137  5.24573
2016-07-14  0.339744  0.549145  2.64316  3.36111  5.66453  5.96368  7.87821
2016-07-15  0.309829  0.459402  2.55342  4.64744  4.46795  6.80128  6.17308
2016-07-16      0.25  0.369658  2.46368  2.67308  4.58761  6.35256  5.63462
2016-07-17  0.279915  0.369658  2.58333  2.91239  4.19872  5.51496  6.65171
like image 395
Kevin Avatar asked Jul 07 '16 21:07

Kevin


2 Answers

Try to cast to float type:

x3.astype(float).round(2)
like image 143
user3217125 Avatar answered Sep 30 '22 06:09

user3217125


as simple as this

df['col_name'] = df['col_name'].astype(float).round(2)
like image 36
Naresh Abburi Avatar answered Sep 30 '22 05:09

Naresh Abburi