How to access matrix table value in python


How can we access the value of a matrix in Python, if I pass its column and row name as input in Python code.

For Example – If I input A as column and B as row, then from the matrix table, I should get its A and B map value.

So far I tried this code, but I was unable to figure out how to get its value:

import numpy as np 
import pandas as pd 
input1= 'B'
input2= 'B'

numpyArray = np.array([[5, 4, 3, 3, 2, 2],  
                        [4, 3, 2, 3, 2, 1],
                        [3, 3, 1, 2, 1, 1],
                        [2, 2, 1, 1, 1, 1]]) 
panda_df = pd.DataFrame(data=numpyArray, index=["A", "B", "C", "D"], 
                                         columns=["A", "B", "C", "D", "E", "F"])


Also, is it possible to achieve this solution in python 2.7?


Taking advantage of your created dataframe, just select according to the inputs:


But you can avoid the dataframe creation by creating a translation from letters to indexes using ord (assuming the inputs will always be capitalized letters, starting from A):

def letter_to_i(letter):
    A -> 0, B -> 1, ...
    return ord(letter) - ord('A')

And now index the array directly:


Source: stackoverflow