numpy, pandas显示小数点问题

numpy

np.around()

In[9]: temp = np.random.random((2,2))

In[10]: temp
Out[10]: 
array([[0.88241156, 0.68553721],
       [0.89101074, 0.57547832]])


In[11]: np.around(temp, decimals=2)  # decimals 保留小数点后 2 数
Out[11]: 
array([[0.88, 0.69],
       [0.89, 0.58]])

np.set_printoptions(precision = n)

In [8]: import numpy as np

In [9]: aa=np.random.rand(2,2)

In [10]: aa
Out[10]:
array([[0.97689966, 0.58643825],
       [0.90758076, 0.53764107]])

In [11]: np.set_printoptions(precision=4)  # 保留小数点后 4 位

In [12]: aa
Out[12]:
array([[0.9769, 0.5864],
       [0.9076, 0.5376]])

pandas

pd.round()

In[18]: data = np.random.random((2,2))

In[19]: df = pd.DataFrame(data)

In[20]: df
Out[20]: 
          0         1
0  0.931845  0.900958
1  0.874922  0.660953

In[21]: df.round(2)  # 保留小数点后 2 位
Out[21]: 
      0     1
0  0.93  0.90
1  0.87  0.66
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值