如何为包含 numpy.ndarray 的 pandas.Datafame 设置显示精度?

问题描述 投票:0回答:0

我正在存储

numpy.ndarray
的浮点数作为
pandas.Datafame
的元素。 我想以自定义精度在 Jupyter Notebook 中显示此类数据框。

最小工作示例(Python 3.9.12):

import numpy as np
import pandas as pd

np.random.seed(12345)

df = pd.DataFrame({
  'a': np.random.rand(1),
  'b': np.random.rand(1),
  'A': [np.random.rand(5)],
  'B': [np.random.rand(5)],
  'C': [np.random.rand(5)],
})

使用

display(df)
打印了太多的小数位并且没有显示整个数组:

    a           b           A                                                   B                                                   C
0   0.929616    0.316376    [0.18391881167709445, 0.2045602785530397, 0.56...   [0.6531770968715709, 0.7489066375339118, 0.653...   [0.9613067360728214, 0.00838829794155349, 0.10..

具有一些任意精度,比如 4 位数字,我想显示如下内容:

    a       b       A                                           B                                   C
0   0.9296  0.3164  [0.1839, 0.2046, 0.5677, 0.5955, 0.9645]    [0.6532, 0.7489, 0.6536, 0.7477]    [0.9613, 0.008388 , 0.1064]

我已经尝试过像

with pd.option_context('display.precision', 4):
with np.printoptions(precision=4):
这样的东西,但它们似乎都没有改变阵列显示的任何东西。

如何设置显示精度为小数点后4位?

python pandas dataframe numpy numpy-ndarray
© www.soinside.com 2019 - 2024. All rights reserved.