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