Python 如何在多索引数据框中选择嵌套列

Python 如何在多索引数据框中选择嵌套列,python,pandas,dataframe,indexing,multi-index,Python,Pandas,Dataframe,Indexing,Multi Index,我创建了一个3D熊猫数据帧,如下所示: A= ['ECFP', 'ECFP', 'ECFP', 'FCFP', 'FCFP', 'FCFP', 'RDK5', 'RDK5', 'RDK5'] B = ['R', 'tau', 'RMSEc', 'R', 'tau', 'RMSEc', 'R', 'tau', 'RMSEc'] C = array([[ 0.1 , 0.3 , 0.5 , nan, 0.6 , 0.4 ], [ 0.4 , 0.3 , 0.3 ,

我创建了一个3D熊猫数据帧,如下所示:

A=  ['ECFP', 'ECFP', 'ECFP', 'FCFP', 'FCFP', 'FCFP', 'RDK5', 'RDK5', 'RDK5']

B = ['R', 'tau', 'RMSEc', 'R', 'tau', 'RMSEc', 'R', 'tau', 'RMSEc']

C = array([[ 0.1 ,  0.3 ,  0.5 ,   nan,  0.6 ,  0.4 ],
       [ 0.4 ,  0.3 ,  0.3 ,   nan,  0.4 ,  0.3 ],
       [ 1.2 ,  1.3 ,  1.1 ,   nan,  1.5 ,  1.  ],
       [ 0.4 ,  0.3 ,  0.4 ,  0.8 ,  0.1 ,  0.2 ],
       [ 0.2 ,  0.3 ,  0.3 ,  0.3 ,  0.5 ,  0.6 ],
       [ 1.  ,  1.2 ,  1.  ,  0.9 ,  1.2 ,  1.  ],
       [ 0.4 ,  0.7 ,  0.5 ,  0.4 ,  0.6 ,  0.6 ],
       [ 0.6 ,  0.5 ,  0.3 ,  0.3 ,  0.3 ,  0.5 ],
       [ 1.2 ,  1.5 ,  1.3 ,  0.97,  1.5 ,  1.  ]])

df = pd.DataFrame(data=C.T, columns=pd.MultiIndex.from_tuples(zip(A,B)))
df = df.dropna(axis=0, how='any')
  ECFP            FCFP            RDK5           
     R  tau RMSEc    R  tau RMSEc    R  tau RMSEc
0  0.1  0.4   1.2  0.4  0.2   1.0  0.4  0.6   1.2
1  0.3  0.3   1.3  0.3  0.3   1.2  0.7  0.5   1.5
2  0.5  0.3   1.1  0.4  0.3   1.0  0.5  0.3   1.3
4  0.6  0.4   1.5  0.1  0.5   1.2  0.6  0.3   1.5
5  0.4  0.3   1.0  0.2  0.6   1.0  0.6  0.5   1.0
最后的数据帧如下所示:

A=  ['ECFP', 'ECFP', 'ECFP', 'FCFP', 'FCFP', 'FCFP', 'RDK5', 'RDK5', 'RDK5']

B = ['R', 'tau', 'RMSEc', 'R', 'tau', 'RMSEc', 'R', 'tau', 'RMSEc']

C = array([[ 0.1 ,  0.3 ,  0.5 ,   nan,  0.6 ,  0.4 ],
       [ 0.4 ,  0.3 ,  0.3 ,   nan,  0.4 ,  0.3 ],
       [ 1.2 ,  1.3 ,  1.1 ,   nan,  1.5 ,  1.  ],
       [ 0.4 ,  0.3 ,  0.4 ,  0.8 ,  0.1 ,  0.2 ],
       [ 0.2 ,  0.3 ,  0.3 ,  0.3 ,  0.5 ,  0.6 ],
       [ 1.  ,  1.2 ,  1.  ,  0.9 ,  1.2 ,  1.  ],
       [ 0.4 ,  0.7 ,  0.5 ,  0.4 ,  0.6 ,  0.6 ],
       [ 0.6 ,  0.5 ,  0.3 ,  0.3 ,  0.3 ,  0.5 ],
       [ 1.2 ,  1.5 ,  1.3 ,  0.97,  1.5 ,  1.  ]])

df = pd.DataFrame(data=C.T, columns=pd.MultiIndex.from_tuples(zip(A,B)))
df = df.dropna(axis=0, how='any')
  ECFP            FCFP            RDK5           
     R  tau RMSEc    R  tau RMSEc    R  tau RMSEc
0  0.1  0.4   1.2  0.4  0.2   1.0  0.4  0.6   1.2
1  0.3  0.3   1.3  0.3  0.3   1.2  0.7  0.5   1.5
2  0.5  0.3   1.1  0.4  0.3   1.0  0.5  0.3   1.3
4  0.6  0.4   1.5  0.1  0.5   1.2  0.6  0.3   1.5
5  0.4  0.3   1.0  0.2  0.6   1.0  0.6  0.5   1.0
如何仅获取所有类型数据的“R”值(“ECFP”、“FCFP”、“RDK5”)之间的相关矩阵?

使用:


使用
slice

df.loc[:,(slice(None),'R')]
Out[375]: 
  ECFP FCFP RDK5
     R    R    R
0  0.1  0.4  0.4
1  0.3  0.3  0.7
2  0.5  0.4  0.5
4  0.6  0.1  0.6
5  0.4  0.2  0.6

两个答案都有效,但首先我必须选择lexstort,否则我会出现以下错误:

KeyError: 'MultiIndex Slicing requires the index to be fully lexsorted tuple len (2), lexsort depth (1)'
解决办法是:

df.sortlevel(axis=1, inplace=True)
print "Correlation matrix of Pearson's R values among all feature vector types:"
df.loc[:, pd.IndexSlice[:, 'R']].corr()