Adding row/column headers to NumPy arrays

后端 未结 5 1460
执笔经年
执笔经年 2020-12-29 03:59

I have a NumPy ndarray to which I would like to add row/column headers.

The data is actually 7x12x12, but I can represent it like this:

5条回答
  •  猫巷女王i
    2020-12-29 04:26

    Numpy will handle n-dimensional arrays fine, but many of the facilities are limited to 2-dimensional arrays. Not even sure how you want the output file to look.

    Many people who would wish for named columns overlook the recarray() capabilities of numpy. Good stuff to know, but that only "names" one dimension.

    For two dimensions, Pandas is very cool.

    In [275]: DataFrame.from_items([('A', [1, 2, 3]), ('B', [4, 5, 6])],
       .....:                      orient='index', columns=['one', 'two', 'three'])
    Out[275]: 
       one  two  three
    A    1    2      3
    B    4    5      6
    

    If output is the only problem you are trying to solve here, I'd probably just stick with a few lines of hand coded magic as it will be less weighty than installing another package for one feature.

提交回复
热议问题