3

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.

0

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, works nicely

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.