您可以按切片器query
的级别使用或选择:MultiIndex
df = pd.DataFrame({'Card':list('baaaaa'),
'Date':['2017-10-01'] * 6,
'Heure':[1,3,5,7,1,0],
'Type':[5,5,5,9,5,9],
'member':list('aaabbb')})
print (df)
Card Date Heure Type member
0 b 2017-10-01 1 5 a
1 a 2017-10-01 3 5 a
2 a 2017-10-01 5 5 a
3 a 2017-10-01 7 9 b
4 a 2017-10-01 1 5 b
5 a 2017-10-01 0 9 b
table = pd.pivot_table(df,index=["Date","member","Card"],
columns="Type",
values="Heure",
aggfunc='size')
print (table)
Type 5 9
Date member Card
2017-10-01 a a 2.0 NaN
b 1.0 NaN
b a 1.0 2.0
table1 = table.query('member == "a"')
print (table1)
Type 5 9
Date member Card
2017-10-01 a a 2.0 NaN
b 1.0 NaN
idx = pd.IndexSlice
table1 = table.loc[idx[:,'a',:],:]
print (table1)
Type 5 9
Date member Card
2017-10-01 a a 2.0 NaN
b 1.0 NaN
编辑:
对于按多个值过滤,请使用:
table1 = table.query('member in ["a", "b"]')
print (table1)
Type 5 9
Date member Card
2017-10-01 a a 2.0 NaN
b 1.0 NaN
b a 1.0 2.0
idx = pd.IndexSlice
table1 = table.loc[idx[:,['a', 'b'],:],:]
print (table1)
Type 5 9
Date member Card
2017-10-01 a a 2.0 NaN
b 1.0 NaN
b a 1.0 2.0