Python 从pd.df.ix[]视图中按行选择?

Python 从pd.df.ix[]视图中按行选择?,python,pandas,python-3.5,Python,Pandas,Python 3.5,给定以下内容,multiIndexedpd.DataFrame: Type p&l position rolldate value vola Date Symbol 2008-01-02 AC 1757.2168 1 1201132800 45588.

给定以下内容,
multiIndex
ed
pd.DataFrame

Type                      p&l position    rolldate        value       vola  
Date       Symbol                                                           
2008-01-02 AC       1757.2168        1  1201132800   45588.9161   480.6781  
           AUD         0.0000        0  1205280000   59872.0044   542.8067  
           BAX       551.1540        2  1208736000  165621.7706   125.8527  
           BTP         0.0000        0          -1          NaN     0.0000  
           C         674.4908        2  1202342400   14407.1226   137.4325  
           CAC40       0.0000        0  1200441600   55565.0000   580.2757  
           CAD         0.0000        0  1205280000   68784.0414   593.7115  
           CC        422.1133        1  1202428800   14276.9608   197.4064  
           CGB       482.2597        1  1203638400   79655.5288   299.6622  
           CHF     -1216.9798       -1  1205280000   76431.4406   391.3853  
           CL          0.0000        0  1200355200   67824.0741  1268.3927  
           COIL        0.0000        0  1199750400   66612.2004  1088.8291  
           CT        296.1601        1  1202774400   23447.7124   239.7177  
           D         217.8649        1  1202688000   13201.2527   210.3416  
           DAX         0.0000        0  1205798400  200712.5000  1644.8412  
           DX        469.7712       -1  1205193600   51749.7277   215.9024  
           EMD         0.0000        0  1205366400   58135.8932   753.5315  
           ES          0.0000        0  1205366400   49649.3736   632.5416  
           ESTX50   -570.0000        1  1205798400   43780.0000   381.5206  
           EUR         0.0000        0  1205280000  125382.9657   605.9757  
           GBL     -1020.0000       -1  1204588800  114130.0000   355.3088  
           GBM      -730.0000       -1  1204588800  108670.0000   229.3634  
           GBP       -93.6138        1  1205280000   84095.0095   477.9144  
           GBS      -825.0000       -3  1204588800  103630.0000   100.1981  
           GBX         0.0000        0  1204588800   91280.0000   548.0709  
           GC          0.0000        0  1201219200   58551.1983   678.5194  
           GE        578.7037        2  1221523200  164760.3486   110.1067  
           GF        204.2484       -1  1203984000   36254.0850   261.8514 
我正试图通过
日期
符号
然后是
名称来访问行的值。 到目前为止,我只做到了这一点:

In [38]: df.ix['2008-01-02', 'AC']
Out[38]: 
       Type     
Value  benchmark             NaN
       cm           1.201824e+09
       cm_next      1.204330e+09
       margin                NaN
       nav                   NaN
       p&l          1.757217e+03
       position     1.000000e+00
       rolldate     1.201133e+09
       value        4.558892e+04
       vola         4.806781e+02
Name: (2008-01-02, AC), dtype: float64
它接近我想要的;然而,我似乎无法理解如何访问
类型

df[df.loc['2008-01-02', 'AC']]['p&l'] # raises a KeyError

In [39]: df.ix['2008-01-02', 'AC']['Value'] # Gets me closer, but not quite there
Out[39]: 
Type
benchmark             NaN
cm           1.201824e+09
cm_next      1.204330e+09
margin                NaN
nav                   NaN
p&l          1.757217e+03
position     1.000000e+00
rolldate     1.201133e+09
value        4.558892e+04
vola         4.806781e+02
Name: (2008-01-02, AC), dtype: float64

In [40]: df.ix['2008-01-02', 'AC']['Value']['p&l'] # raises another KeyError
我不能依赖于
DataFrame.head()
.tail()
或任何其他类型的数字索引,因为我不能确定列的顺序始终相同,也不能保证每次运行的列数相等。

我想您可以使用-请参阅:

编辑:

如果
多索引
也在列中,则添加第一级-

print df
Type                  Value                                            
Date                    p&l position    rolldate        value      vola
2008-01-02 AC     1757.2168        1  1201132800   45588.9161  480.6781
           AUD       0.0000        0  1205280000   59872.0044  542.8067
           BAX     551.1540        2  1208736000  165621.7706  125.8527
           BTP       0.0000        0          -1          NaN    0.0000
           C       674.4908        2  1202342400   14407.1226  137.4325
           CAC40     0.0000        0  1200441600   55565.0000  580.2757
           CAD       0.0000        0  1205280000   68784.0414  593.7115
           CC      422.1133        1  1202428800   14276.9608  197.4064
           CGB     482.2597        1  1203638400   79655.5288  299.6622


idx = pd.IndexSlice
print df.loc[idx['2008-01-02', 'AC'], idx['Value','rolldate']]
1201132800.0

df.loc[idx['2008-01-02','AC'],idx['rolldate']]
为我提出了一个
KeyError
:标签[rolldate]不在[索引]中。什么是
打印df.columns
?它是列表吗?它是一个
多索引
-
多索引(级别=['Value'],['benchmark',cm',cm_next',margin',nav',p&l',position',rolldate',Value',vola']],标签=[[0,0,0,0,0,0,0,0],[0,1,2,3,4,5,6,7,8,9]],名称=[None',Type'])
print df
Type                  Value                                            
Date                    p&l position    rolldate        value      vola
2008-01-02 AC     1757.2168        1  1201132800   45588.9161  480.6781
           AUD       0.0000        0  1205280000   59872.0044  542.8067
           BAX     551.1540        2  1208736000  165621.7706  125.8527
           BTP       0.0000        0          -1          NaN    0.0000
           C       674.4908        2  1202342400   14407.1226  137.4325
           CAC40     0.0000        0  1200441600   55565.0000  580.2757
           CAD       0.0000        0  1205280000   68784.0414  593.7115
           CC      422.1133        1  1202428800   14276.9608  197.4064
           CGB     482.2597        1  1203638400   79655.5288  299.6622


idx = pd.IndexSlice
print df.loc[idx['2008-01-02', 'AC'], idx['Value','rolldate']]
1201132800.0