How to pass through a list of queries to a pandas dataframe, and output the list of results?
<p>When selecting rows whose column value <code>column_name</code> equals a scalar, <code>some_value</code>, we use <code>==:</code></p>

<pre><code>df.loc[df['column_name'] == some_value]
</code></pre>

<p>or use <code>.query()</code> </p>

<pre><code>df.query('column_name == some_value')
</code></pre>

<p>In a concrete example:</p>

<pre><code>import pandas as pd
import numpy as np
df = pd.DataFrame({'Col1': 'what are men to rocks and mountains'.split(),
                   'Col2': 'the curves of your lips rewrite history.'.split(),
                   'Col3': np.arange(7),
                   'Col4': np.arange(7) * 8})

print(df)

         Col1      Col2  Col3  Col4
0       what       the     0     0
1        are    curves     1     8
2        men        of     2    16
3         to      your     3    24
4      rocks      lips     4    32
5        and   rewrite     5    40
6  mountains  history      6    48
</code></pre>

<p>A query could be</p>

<pre><code>rocks_row = df.loc[df['Col1'] == "rocks"]
</code></pre>

<p>which outputs</p>

<pre><code>print(rocks_row)
    Col1  Col2  Col3  Col4
4  rocks  lips     4    32
</code></pre>

<p>I would like to pass through a list of values to query against a dataframe, which outputs a list of "correct queries". </p>

<p>The queries to execute would be in a list, e.g.</p>

<pre><code>list_match = ['men', 'curves', 'history']
</code></pre>

<p>which would output all rows which meet this condition, i.e. </p>

<pre><code>matches = pd.concat([df1, df2, df3]) 
</code></pre>

<p>where </p>

<pre><code>df1 = df.loc[df['Col1'] == "men"]

df2 = df.loc[df['Col1'] == "curves"]

df3 = df.loc[df['Col1'] == "history"]
</code></pre>

<p>My idea would be to create a function that takes in a </p>

<pre><code>output = []
def find_queries(dataframe, column, value, output):
    for scalar in value: 
        query = dataframe.loc[dataframe[column] == scalar]]
        output.append(query)    # append all query results to a list
    return pd.concat(output)    # return concatenated list of dataframes
</code></pre>

<p>However, this appears to be exceptionally slow, and doesn't actually take advantage of the pandas data structure. What is the "standard" way to pass through a list of queries through a pandas dataframe? </p>

<p>EDIT: How does this translate into "more complex" queries in pandas? e.g. <code>where</code> with an HDF5 document? </p>

<pre><code>df.to_hdf('test.h5','df',mode='w',format='table',data_columns=['A','B'])

pd.read_hdf('test.h5','df')

pd.read_hdf('test.h5','df',where='A=["foo","bar"] &amp; B=1')
</code></pre>

<p>The best way to deal with this is by indexing into the rows using a Boolean series as you would in R.</p>

<p>Using your df as an example,</p>

<pre><code>In [5]: df.Col1 == "what"
Out[5]:
0     True
1    False
2    False
3    False
4    False
5    False
6    False
Name: Col1, dtype: bool

In [6]: df[df.Col1 == "what"]
Out[6]:
   Col1 Col2  Col3  Col4
0  what  the     0     0
</code></pre>

<p>Now we combine this with the pandas isin function.</p>

<pre><code>In [8]: df[df.Col1.isin(["men","rocks","mountains"])]
Out[8]:
        Col1      Col2  Col3  Col4
2        men        of     2    16
4      rocks      lips     4    32
6  mountains  history.     6    48
</code></pre>

<p>To filter on multiple columns we can chain them together with &amp; and | operators like so.</p>

<pre><code>In [10]: df[df.Col1.isin(["men","rocks","mountains"]) | df.Col2.isin(["lips","your"])]
Out[10]:
        Col1      Col2  Col3  Col4
2        men        of     2    16
3         to      your     3    24
4      rocks      lips     4    32
6  mountains  history.     6    48

In [11]: df[df.Col1.isin(["men","rocks","mountains"]) &amp; df.Col2.isin(["lips","your"])]
Out[11]:
    Col1  Col2  Col3  Col4
4  rocks  lips     4    32
</code></pre>

python python-3.x pandas dataframe