2

我想根据具体索引过滤数据框。我已经阅读了有关查询的内容,但没有成功。

这是创建我的数据透视表的代码。我想过滤特定成员

    df = pd.DataFrame(my_dataframe)
    table = pd.pivot_table(df,index=["Date","member","Card"], columns=["Type"],values=["Heure"],aggfunc=[len]) #,fill_value=0)
    table.to_excel(writer, sheet_name='TcD')

我应该怎么办 ?

谢谢

4

1 回答 1

0

您可以按切片器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
于 2018-02-25T10:36:40.797 回答