5

I've got an 18x18 2d numpy array (it's a confusion matrix)...and I need/would like to display it as a table in an ipython notebook.

When I simply print it out, it displays with overlap--the rows are so long they take up two lines.

Is there a library that will allow me to print this array in a sort of spreadsheet format?

wjandrea
  • 23,210
  • 7
  • 49
  • 68
Chris
  • 25,362
  • 24
  • 71
  • 129

2 Answers2

13

You can use Pandas for that.

import pandas as pd
print pd.DataFrame(yourArray)
-2

Note: Konstantinos proposal holds only for 1-D and 2-D arrays!

You can use numpy.array2string():

from pprint import pprint
import numpy as np

array = np.array([[1,2,3], [4,5,6]])
print(np.array2string(array).replace('[[',' [').replace(']]',']'))

Output:

 [1 2 3]
 [4 5 6]

See also: Printing Lists as Tabular Data

guyomd
  • 12
  • 4