2

This is a simplifeid version of my database.

                     A   B
Store Product Year        
A     1       2014  12  63
      2       2015  32   5
      3       2016  45   0
B     1       2014  45   0
      2       2015   6  19
      3       2016   7   4
C     1       2014  98   5
      2       2015  54  43
      3       2016  28   0
D     1       2014   4   6
      2       2015  32  67
      3       2016   1   5 

I am trying to select all stores for where the value in column B in 2016 is zero. So in this instance I wish to select store A and C.

df = df[df['B']==0]

Only selects the individual rows where B = 0. Whereas want I want is to select this

                     A   B
Store Product Year        
A     1       2014  12  63
      2       2015  32   5
      3       2016  45   0
C     1       2014  98   5
      2       2015  54  43
      3       2016  28   0

Thanks in advance for help or advice.

jyh5
  • 77
  • 1
  • 9

1 Answers1

3

You can use index.get_level_values to create a mask then boolean indexing:

mask = df[(df.index.get_level_values('Year') == 2016) & (df['B']==0)].index.get_level_values('Store')
df[df.index.get_level_values('Store').isin(mask)]

                     A   B
Store Product Year        
A     1       2014  12  63
      2       2015  32   5
      3       2016  45   0
C     1       2014  98   5
      2       2015  54  43
      3       2016  28   0
cs95
  • 379,657
  • 97
  • 704
  • 746
It_is_Chris
  • 13,504
  • 2
  • 23
  • 41