Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Structured 2D Numpy Array: setting column and row names

I'm trying to find a nice way to take a 2d numpy array and attach column and row names as a structured array. For example:

import numpy as np

column_names = ['a', 'b', 'c']
row_names    = ['1', '2', '3']

matrix = np.reshape((1, 2, 3, 4, 5, 6, 7, 8, 9), (3, 3))

# TODO: insert magic here

matrix['3']['a']  # 7

I've been able to use set the columns like this:

matrix.dtype = [(n, matrix.dtype) for n in column_names]

This lets me do matrix[2]['a'] but now I want to rename the rows so I can do matrix['3']['a'].

like image 754
freebie Avatar asked Jun 22 '17 20:06

freebie


People also ask

How do you select rows and columns in NumPy array?

We can use [][] operator to select an element from Numpy Array i.e. Example 1: Select the element at row index 1 and column index 2. Or we can pass the comma separated list of indices representing row index & column index too i.e.

Are NumPy arrays row column or column row?

Data in NumPy arrays can be accessed directly via column and row indexes, and this is reasonably straightforward.


1 Answers

As far as I know it's not possible to "name" the rows with pure structured NumPy arrays.

But if you have pandas it's possible to provide an "index" (which essentially acts like a "row name"):

>>> import pandas as pd
>>> import numpy as np
>>> column_names = ['a', 'b', 'c']
>>> row_names    = ['1', '2', '3']

>>> matrix = np.reshape((1, 2, 3, 4, 5, 6, 7, 8, 9), (3, 3))
>>> df = pd.DataFrame(matrix, columns=column_names, index=row_names)
>>> df
   a  b  c
1  1  2  3
2  4  5  6
3  7  8  9

>>> df['a']['3']      # first "column" then "row"
7

>>> df.loc['3', 'a']  # another way to index "row" and "column"
7
like image 159
MSeifert Avatar answered Oct 26 '22 09:10

MSeifert