How to use a dict to subset a DataFrame?
Question: Say, I have given a DataFrame with most of the columns being categorical data.

    
    
    > data.head()
      age risk     sex smoking
    0  28   no    male      no
    1  58   no  female      no
    2  27   no    male     yes
    3  26   no    male      no
    4  29  yes  female     yes
    

And I would like to subset this data by a dict of key-value pairs for those
categorical variables.

    
    
    tmp = {'risk':'no', 'smoking':'yes', 'sex':'female'}
    

Hence, I would like to have the following subset.

    
    
    data[ (data.risk == 'no') & (data.smoking == 'yes') & (data.sex == 'female')]
    

What I want to do is:

    
    
    data[tmp]
    

What is the most python / pandas way of doing this?

* * *

Minimal example:

    
    
    import numpy as np
    import pandas as pd
    from pandas import Series, DataFrame
    
    x = Series(random.randint(0,2,50), dtype='category')
    x.cat.categories = ['no', 'yes']
    
    y = Series(random.randint(0,2,50), dtype='category')
    y.cat.categories = ['no', 'yes']
    
    z = Series(random.randint(0,2,50), dtype='category')
    z.cat.categories = ['male', 'female']
    
    a = Series(random.randint(20,60,50), dtype='category')
    
    data = DataFrame({'risk':x, 'smoking':y, 'sex':z, 'age':a})
    
    tmp = {'risk':'no', 'smoking':'yes', 'sex':'female'}
    


Answer: You can create a look up data frame from the dictionary and then do an inner
join with the `data` which will have the same effect as `query`:

    
    
    from pandas import merge, DataFrame
    merge(DataFrame(tmp, index =[0]), data)
    

[![enter image description
here](https://i.stack.imgur.com/xW4kf.png)](https://i.stack.imgur.com/xW4kf.png)

