Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to round up a column in pandas

Tags:

python

pandas

How to round up the values of column in pandas. I need to round up the values of column.

     hostname    ip           date      cpu  cpumax  
0    mysql      192.168.1.1  22-Aug-19  12.0   10.33   
1    cassandra  192.168.2.1  22-Aug-19   2.0   92.43  

My Code is below which is working fine.

 import pandas as pd
 import numpy as np
 import math
 data_file['cpumax'] = np.ceil((data_file['cpumax'])+0.5)

My cpumax column is

cpumax
11
93

Is there any alternative like math.ceil. Since math.ceil I can't apply here since its not Series

like image 820
sre Avatar asked Jan 28 '26 19:01

sre


2 Answers

np.ceil should do it but if you still want to use math.ceil use

df['cpumax'] = df['cpumax'].apply(math.ceil)

like image 172
aunsid Avatar answered Jan 31 '26 09:01

aunsid


Import numpy as np

df['cpumax'] = np.ceil(df['cpumax']).astype(int)
like image 36
moys Avatar answered Jan 31 '26 09:01

moys