如何在pandas中对列进行四舍五入

3
如何在 Pandas 中对列的值进行四舍五入。我需要将列的值四舍五入。
     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  

我的代码如下,运行良好。
 import pandas as pd
 import numpy as np
 import math
 data_file['cpumax'] = np.ceil((data_file['cpumax'])+0.5)

我的cpumax列是

cpumax
11
93

是否有其他类似于math.ceil的替代方法。由于其不是序列,因此无法在这里应用math.ceil


2
为什么你不想使用np.ceil呢?你的代码完全正常运行。 - undefined
2
为什么在这里加上 0.5 - undefined
2个回答

3

使用np.ceil函数即可,但如果您仍想使用math.ceil,请使用以下代码

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


2

导入numpy库,别名为np

df['cpumax'] = np.ceil(df['cpumax']).astype(int)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接