Python 将两个numpy数组打印为表的最简单方法

Python 将两个numpy数组打印为表的最简单方法,python,numpy,Python,Numpy,我有 我需要将其输出为表: x = np.array([1, 4, 5, ...100 more values]) y = np.array([1.23452324, 6.2341238, 11.1348219, ...100 more values]) 我需要垂直打印数组,右边对齐,值应该四舍五入到3位小数。有什么优雅的方式吗 附:我试过这个: 1 1.235 4 6.234 5 11.135 但它不起作用。您可以使用pandas: for i in range(n):

我有

我需要将其输出为表:

x = np.array([1, 4, 5, ...100 more values])

y = np.array([1.23452324, 6.2341238, 11.1348219, ...100 more values])
我需要垂直打印数组,右边对齐,值应该四舍五入到3位小数。有什么优雅的方式吗

附:我试过这个:

1   1.235
4   6.234
5  11.135
但它不起作用。

您可以使用pandas:

for i in range(n):
    print(repr(x.item(i)).rjust((7)), repr(y.item(i)).rjust(7), end=' ')
    print(repr(z.item(i)).rjust(7))
查阅Pandas文档,了解如何根据需要设置数字格式。具体来看:

确保您在机器上安装了熊猫:

蟒蛇2/蟒蛇3:

>>> df.round(3)
   x       y
0  1   1.235
1  4   6.234
2  5  11.135
开始之前,请安装制表软件包

从终端:

>>>import numpy as np 

>>>x = np.array([1, 2, 3, 4, 5, 6])
>>>y = np.array([12.3424, 323.1234, 125.4342, 342.1234, 654.4342, 234.3434])

>>>for x, y in zip(x, y):
       print('{0}\t{1:.1f}'.format(x, y))

    1     12.3
    2     323.1
    3     125.4
    4     342.1
    5     654.4
    6     234.3
那么剩下的将是:

conda install tabulate

您是否尝试过通过您的研究实施任何方法?出了什么问题?请张贴您尝试过的不优雅方式!!几乎是我想要的!如果你能告诉我如何对这些值进行四舍五入,我会非常高兴thankful@GeorgeZorikov更新为roundingNow有一点奇怪的输出调整。我认为熊猫非常适合我的情况。无论如何谢谢你!要使用逗号对齐和其他格式选项。。。尝试对于listzipx中的xy,y:print'{0:>3.0f}{1:>6.1f}'。格式*xyWow!这就是我说的。非常感谢。没问题。我更新了我的答案,包括四舍五入。也很好用。谢谢你回答我的问题!
>>>import numpy as np 

>>>x = np.array([1, 2, 3, 4, 5, 6])
>>>y = np.array([12.3424, 323.1234, 125.4342, 342.1234, 654.4342, 234.3434])

>>>for x, y in zip(x, y):
       print('{0}\t{1:.1f}'.format(x, y))

    1     12.3
    2     323.1
    3     125.4
    4     342.1
    5     654.4
    6     234.3
conda install tabulate
import numpy as np
from tabulate import tabulate


x = np.array([1, 4, 5, 100])
y = np.array([1.23452324, 6.2341238, 11.1348219, 100])

col_headers = ["x", "y"]

merged_array = np.array([x, y]).T


table = tabulate(merged_array , col_headers, tablefmt="fancy_grid", floatfmt = ".2f")

print(table)
╒════════╤════════╕
│      x │      y │
╞════════╪════════╡
│   1.00 │   1.23 │
├────────┼────────┤
│   4.00 │   6.23 │
├────────┼────────┤
│   5.00 │  11.13 │
├────────┼────────┤
│ 100.00 │ 100.00 │
╘════════╧════════╛