Selecting data from Pandas dataframe based on criteria stored in a dict
<p>I have a Pandas dataframe that contains a large number of variables. This can be simplified as:</p>

<pre><code>tempDF = pd.DataFrame({ 'var1': [12,12,12,12,45,45,45,51,51,51],
                        'var2': ['a','a','b','b','b','b','b','c','c','d'],
                        'var3': ['e','f','f','f','f','g','g','g','g','g'],
                        'var4': [1,2,3,3,4,5,6,6,6,7]})
</code></pre>

<p>If I wanted to select a subset of the dataframe (e.g. var2='b' and var4=3), I would use:</p>

<pre><code>tempDF.loc[(tempDF['var2']=='b') &amp; (tempDF['var4']==3),:]
</code></pre>

<p>However, is it possible to select a subset of the dataframe if the matching criteria are stored within a dict, such as:</p>

<pre><code>tempDict = {'var2': 'b','var4': 3}
</code></pre>

<p>It's important that the variable names are not predefined and the number of variables included in the dict is changeable.</p>

<p>I've been puzzling over this for a while and so any suggestions would be greatly appreciated.</p>