gpt4 book ai didi

python - 如何仅编辑一个数据帧pandas的显示精度

转载 作者:行者123 更新时间:2023-12-01 08:28:50 26 4
gpt4 key购买 nike

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

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

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

关于python - 如何仅编辑一个数据帧pandas的显示精度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54034755/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com