Filtering a pandas data frame with a variable
<p>I'm stuck on something simple but I can't see it despite reading the docs and relevant SO questions. This involves filtering records from data that comes out of a WordPress database. </p>

<p>Create some data:</p>

<pre><code>import pandas as pd

data = {'number': [1,2,3],\
        'field': ['billing_last_name', 'shipping_last_name', 'field_1435'],\
        'name': ['jones', 'smith', 'jones']}
dframe = pd.DataFrame(data)
print dframe

                field   name  number
0   billing_last_name  jones       1
1  shipping_last_name  smith       2
2          field_1435  jones       3
</code></pre>

<p>Select a subset of the data:</p>

<pre><code>field_filter = 'billing_last_name'
number_filter = 1
choice = dframe[(dframe['field'] == field_filter) &amp; (dframe['number'] == number_filter)]
print choice

               field   name  number
0  billing_last_name  jones       1
0    jones
Name: name, dtype: object
</code></pre>

<p>Use this result to set a variable for further filtering:</p>

<pre><code>match = str(choice['name'])
</code></pre>

<p>Here's where the problem starts. If I filter with the variable it returns nothing:</p>

<pre><code>print dframe[dframe['name'] == match]

Empty DataFrame
Columns: [field, name, number]
Index: []    
</code></pre>

<p>If I run the same filter with the string the variable holds it returns the correct result:</p>

<pre><code>print dframe[dframe['name'] == 'jones']

               field   name  number
0  billing_last_name  jones       1
2         field_1435  jones       3
</code></pre>

<p>Yet both the variable and its contents are strings, apparently:</p>

<pre><code>print type('jones')
print type(match)

&lt;type 'str'&gt;
&lt;type 'str'&gt;
</code></pre>

<p>Why doesn't the filter with the variable work?</p>

<p>Maybe you should create match as:</p>

<pre><code>match = str(choice['name'][0])
</code></pre>

<p>cause <strong>choice['name']</strong> is Series</p>

python pandas