java中round函数怎么用,Python pandas.DataFrame.round函数方法的使用

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值