2017-02-17 256 views
0

提取数据我有数据的多指标熊猫数据帧如下:从熊猫多索引数据帧

dataMyDF.ix[0:3] 
                dataMyDF 
col1 col2   col3  col4 col5   
W  W   8.0  29.0 4.0  0.291155 
P  I   8.0  29.0 7.0  0.108057 
N  D   2.0  14.0 16.0  0.247355 

其中COL1通过COL5是索引和数据是的值。多指标可以被看作是:

shape.index[0] 
(u'W', u'W', 8.0, 29.0, 4.0) 

如何从dataMyDF提取数据:

即我想要做的事,如:

indexArr = [(W, W, 8.0, 29.0, 4.0), (N, D, 2.0, 14.0, 16.0)] 

dataMyDF[indexArr]得到[0.291155, 0.247355]

回答

1

使用loc

In [1062]: df.loc[indexArr] 
Out[1062]: 
          dataMyDF 
col1 col2 col3 col4 col5 
W W 8.0 29.0 4.0 0.291155 
N D 2.0 14.0 16.0 0.247355 

In [1063]: df.loc[indexArr].values 
Out[1063]: 
array([[ 0.291155], 
     [ 0.247355]]) 

.ix inplace of .loc works fine too。