Delete rows pandas Dataframe based on index (multiple criteria) (Python 3.5.1)
Question: Suppose I have a Pandas DataFrame with MultiIndex on rows. How can I delete
rows based on the value of one of the levels of the index based on multiple
criteria?

For example, suppose I have

    
    
    import pandas as pd
    
    df = {'population': [100, 200, 300, 400, 500, 600, 700, 800]}
    arrays = [['NJ', 'NJ', 'NY', 'NY', 'CA', 'CA', 'NV', 'NV'],
              ['A', 'B', None, 'D', 'E', 'F', None, 'G']]
    tuples = list(zip(*arrays))
    index = pd.MultiIndex.from_tuples(tuples, names=['state', 'county'])
    
    df = pd.DataFrame(df, index=index)
    
                       population
    state   county  
    NJ        A          100
              B          200
    NY        NaN        300
              D          400
    CA        E          500
              F          600
    NV        NaN        700
              G          800   
    

I want to delete all rows where the `county` level of the index is NaN and
also delete it when it is equal to 'D' and 'G'. In other words, I want to end
up with a DataFrame

    
    
                       population
    state   county  
    NJ        A          100
              B          200
              D          400
    CA        E          500
              F          600  
    

So the following sort of works:

    
    
    df = df.iloc[df.index.get_level_values('county') != 'D']
    df = df.iloc[df.index.get_level_values('county') != 'G']
    

But the problem is that in my real use case there is several of these
criteria. Also, I can't seem to find a way to delete NaN's using this method.

Thanks!


Answer: Call [`drop`](http://pandas.pydata.org/pandas-
docs/stable/generated/pandas.DataFrame.drop.html#pandas.DataFrame.drop) and
pass a list on `level='county` to drop row labels with those values on that
index level:

    
    
    In [284]:
    df.drop(['D','G',np.NaN], level='county')
    
    Out[284]:
                  population
    state county            
    NJ    A              100
          B              200
    CA    E              500
          F              600
    

