如何获取级别秒数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`
答案 0 :(得分:1)
我认为需要通过loc
和tuple
进行选择,以返回一行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