如何仅编辑一个数据帧熊猫的显示精度



我想编辑特定数据帧的显示精度。

现在我看到有人说你可以使用这样的东西:

pd.set_option('precision', 5)

但是,如何确保只有一个特定数据帧使用此精度,而另一个数据帧保持原样?另外,是否可以仅更改特定列的此精度?

一种方法是浮点列的字符串表示:

np.random.seed(2019)
df = pd.DataFrame(np.random.rand(5,3), columns=list('abc'))
print (df)
          a         b         c
0  0.903482  0.393081  0.623970
1  0.637877  0.880499  0.299172
2  0.702198  0.903206  0.881382
3  0.405750  0.452447  0.267070
4  0.162865  0.889215  0.148476
df['a'] = df['a'].map("{:,.15f}".format)
print (df)
                   a         b         c
0  0.903482214419274  0.393081  0.623970
1  0.637877401022227  0.880499  0.299172
2  0.702198270186552  0.903206  0.881382
3  0.405749797979913  0.452447  0.267070
4  0.162864870291925  0.889215  0.148476
print (df.dtypes)
a     object
b    float64
c    float64
dtype: object

此方法不会影响原始数据帧。
df.style.set_precision(5)

对于一列,您可以使用
df.style.format({'Column_Name': "{:.5f}"})

最新更新