我知道有人问过类似的问题,但我似乎找不到解决方案。
使用以下代码,我可以使用列和第一个索引过滤掉,但不能过滤第二个索引。
import pandas as pd
import numpy as np
ix = pd.MultiIndex.from_product([ ['foo', 'bar'], ['baz',
'can']], names=['a', 'b'])
data = np.arange(len(ix))
df = pd.DataFrame(data, index=ix, columns=['values'])
df['values2']=[1,4,5,6]
print(df)
结果输出如下:
注意最后一行如何工作
df.loc['foo','can']['values2'] # works
df.loc['foo']['values2'] # works
df.loc['foo','can'][:] # works
df.loc['foo',:][:] # works
df.loc[:,'can'][:] # does not work.
答案 0 :(得分:3)
使用{{3}}进行更复杂的选择:
idx = pd.IndexSlice
print (df.loc[idx['foo', 'can'], 'values'])
1
print (df.loc[idx['foo'], 'values'])
b
baz 0
can 1
Name: values, dtype: int32
print (df.loc[idx['foo',:], 'values'])
a b
foo baz 0
can 1
Name: values, dtype: int32
print (df.loc[idx['foo','can'], :])
values 1
values2 4
Name: (foo, can), dtype: int64
print (df.loc[idx['foo',:], :])
values values2
a b
foo baz 0 1
can 1 4
print (df.loc[idx[:, 'can'], :])
values values2
a b
foo can 1 4
bar can 3 6