title: Pandas: How to drop list of indices of Level 2 in MultiIndex DataFrame
body: <p>I have a Pandas dataframe shaped as follows:</p>
  
  <p><a href="https://i.stack.imgur.com/mmHjP.png" rel="nofollow noreferrer">Dataframe Visualization</a></p>
  
  <p>What I would like to do is for every index of Level 1 (2000, ... , 2020), drop the Level 2 indices that are contained in a list,  'inactive stations', effectively removing the rows associated to the indices from the dataset. </p>
  
  <p>My code for this is as follows:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>#getting list of level 1 one values without duplicates
  level_1_values = list(dict.fromkeys(ds2.index.get_level_values(0).tolist()))
  
  #removing all level 2 indices in inactive_stations 
  for value in level_1_values:
      #getting Level 2 indices for each Level 1 index
      station_list = ds2.loc[[value]].index.get_level_values(1).tolist()
      #calculating list of indices to be removed, 'inactive_sessions'
      inactive_stations = set(station_list).difference(set(active_stations))
      #dropping rows associated to inactive stations
      ds2.index.set_levels(ds2.loc[[value]].index.levels[1].drop(inactive_stations), level=1)
      </code></pre>
  </div>
  </div>
  </p>
  
  <p>I get the following error:</p>
  
  <p>ValueError: On level 1, code max (33721) >= length of level (22196). NOTE: this index is in an inconsistent state</p>