How do I print an aligned numpy array with (text) row and column labels?

后端 未结 4 1064
轮回少年
轮回少年 2020-12-31 08:22

Is there any elegant way to exploit the correct spacing feature of print numpy.array to get a 2D array, with proper labels, that aligns properly? For example, g

4条回答
  •  礼貌的吻别
    2020-12-31 08:31

    You can use IPython notebook + Pandas for that. Type your original example in IPython notebook:

    import numpy
    x = numpy.array([[85, 86, 87, 88, 89], 
                     [90, 191, 192, 93, 94], 
                     [95, 96, 97, 98, 99], 
                     [100,101,102,103,104]])
    
    row_labels = ['Z', 'Y', 'X', 'W']
    column_labels = ['A', 'B', 'C', 'D', 'E']
    

    Then create a DataFrame:

    import pandas
    df = pandas.DataFrame(x, columns=column_labels, index=row_labels)
    

    And then view it:

    enter image description here

提交回复
热议问题