2016-04-24 104 views
0

有沒有什麼方法可以創建非數字索引的二維矩陣?更重要的是通過使用這些非數字指標來獲取/設置值?多維矩陣(列表)中的非數字索引

我的意思是具有像

mat= 
     one two three 
    A 1 2  3 
    B 4 5  6 

2D矩陣,然後能夠SET/GET像

>>>m13=mat[A,three] 
    >>> m13 
    >>> 3 

任何想法片斷理解。我正在考慮使用'熊貓',但無法弄清楚。

回答

3

您可以使用loc

import pandas as pd 

mat = pd.DataFrame({'three': {'A': 3, 'B': 6}, 
        'two': {'A': 2, 'B': 5}, 
        'one': {'A': 1, 'B': 4}}, 
        columns = ['one','two','three']) 

print mat 
    one two three 
A 1 2  3 
B 4 5  6 

#GET 
print mat.loc['A', 'three'] 
3 

#SET 
mat.loc['A', 'three'] = 10 
print mat 
    one two three 
A 1 2  10 
B 4 5  6 

編輯:

您可以創建DataFramenumpy array太:

print arr 
[[1 2 3] 
[4 5 6]] 

mat = pd.DataFrame(arr, index=['A','B'], columns=['one','two','three']) 
    one two three 
A 1 2  3 
B 4 5  6 
+0

謝謝回答,你能告訴你如何創建 '墊子' ? – Rebin

+0

當然,答案是編輯。 – jezrael

+0

感謝您提供及時的解決方案。 – Rebin