DataFrame.round(decimals=0, *args, **kwargs) [source]
将DataFrame舍入到位数可变的小数。
参数:decimals :int, dict, Series
将每一列四舍五入的小数位数。如果给定了int,
则将每列四舍五入到相同的位置。否则,
dict和Series将四舍五入到可变数位。
如果小数点是类似于骰子的,
则列名应该在键中;如果小数是Series,
则应该在索引中。
任何未包含在小数中的列将保留原样。
不属于输入列的小数元素将被忽略。
*args :其他关键字没有影响,
但可以接受与numpy的兼容性。
**kwargs:其他关键字没有影响,
但可以接受与numpy的兼容性。
返回值:DataFrame
将受影响的列四舍五入到指定的小数位数的DataFrame。
例子>>> df = pd.DataFrame([(.21, .32), (.01, .67), (.66, .03), (.21, .18)],
... columns=['dogs', 'cats'])
>>> df
dogs cats
0 0.21 0.32
1 0.01 0.67
2 0.66 0.03
3 0.21 0.18
通过提供整数,每一列都舍入到相同的小数位数>>> df.round(1)
dogs cats
0 0.2 0.3
1 0.0 0.7
2 0.7 0.0
3 0.2 0.2
使用dict时,可以指定特定列的位数,列名称为键,小数位数为值>>> df.round({'dogs': 1, 'cats': 0})
dogs cats
0 0.2 0.0
1 0.0 1.0
2 0.7 0.0
3 0.2 0.0
使用Series,可以指定特定列的位数,以列名称为索引,小数位数为值>>> decimals = pd.Series([0, 1], index=['cats', 'dogs'])
>>> df.round(decimals)
dogs cats
0 0.2 0.0
1 0.0 1.0
2 0.7 0.0
3 0.2 0.0