如何使用iloc获取二级秒的索引值以减去A和B

时间:2018-08-03 06:47:01

标签: python pandas numpy-ndarray

如何获取级别秒数2的索引值以在第一级的每个级别减去A和B

bar  two     -0.673690  0.113648


 first  sec      A         B
bar     one     -0.424972  0.567020  
        two     -0.673690  0.113648 
baz     one      0.404705  0.577046 
        two     -0.370647 -1.157892 
foo     one      1.075770 -0.109050  
        two      0.357021 -0.674600 
qux     one     -1.294524  0.413738 
        two     -0.013960 -0.362543`

1 个答案:

答案 0 :(得分:1)

我认为需要通过loctuple进行选择,以返回一行DataFrame添加[]

df = df.loc[[('bar','two')]]
print (df)
                 A         B
first sec                   
bar   two -0.67369  0.113648

或针对Series

s = df.loc[('bar','two')]
#alternative
#s = df.xs(('bar','two'))
print (s)
A    -0.67369
B    0.113648
Name: (bar, two), dtype: object

但是如果需要tuple的位置,请使用Index.get_loc

i = df.index.get_loc(('bar','two'))
print (i)
1