Pandas: Query tool doesn't work if column headers are tuples: TypeError: argument of type 'int' is not iterable
<p>TLDR: The df.query() tool doesn't seem to work if the df's columns are tuples or even tuples converted into strings. How can I work around this to get the slice I'm aiming for?</p>

<hr>

<p><strong>Long Version:</strong> I have a pandas dataframe that looks like this (although there are a lot more columns and rows...):</p>

<pre><code>&gt; dosage_df

Score   ("A_dose","Super")  ("A_dose","Light")  ("B_dose","Regular")
28              1               40              130
11              2               40              130
72              3               40              130
67              1               90              130
74              2               90              130
89              3               90              130
43              1               40              700
61              2               40              700
5               3               40              700
</code></pre>

<p>Along with my data frame, I also have a python dictionary with the relevant ranges for each feature. The keys are the feature names, and the different values which it can take are the keys:</p>

<pre><code># Original Version 

dosage_df.columns = ['First Score', 'Last Score', ("A_dose","Super"), ("A_dose","Light"), ("B_dose","Regular")] 

dict_of_dose_ranges = {("A_dose","Super"):[1,2,3],
                       ("A_dose","Light"):[40,70,90],
                       ("B_dose","Regular"):[130,200,500,700]}
</code></pre>

<p>For my purposes, I need to generate a particular combination (say A_dose = 1, B_dose = 90, and C_dose = 700), and based on those settings take the relevant slice out of my dataframe, and do relevant calculations from that smaller subset, and save the results somewhere.</p>

<p>I'm doing this by implementing the following:</p>

<pre><code>from itertools import product

for dosage_comb in product(*dict_of_dose_ranges.values()):
    dosage_items = zip(dict_of_dose_ranges.keys(), dosage_comb)
    query_str = ' &amp; '.join('{} == {}'.format(*x) for x in dosage_items)
    **sub_df = dosage_df.query(query_str)**
    ...
</code></pre>

<p><strong>The problem is that is gets hung up on the query step, as it returns the following error message:</strong></p>

<pre><code>TypeError: argument of type 'int' is not iterable
</code></pre>

<p>In this case, the query generated looks like this:</p>

<p>query_str = "("A_dose","Light") == 40 &amp; ("A_dose","Super") == 1 &amp; ("B_dose","Regular") == 130"</p>

<p><strong>Troubleshooting Attempts:</strong></p>

<p>I've confirmed that indeed that solution should work for a dataframe with just string columns as found <a href="http://stackoverflow.com/questions/39378510/iterating-across-multiple-colums-in-pandas-df-and-slicing-dynamically">here</a>. In addition, I've also tried "tricking" the tool by converting the columns and the dictionary keys into strings by the following code... but that returned the same error.</p>

<pre><code># String Version
dosage_df.columns = ['First Score', 'Last Score', '("A_dose","Super")', '("A_dose","Light")', '("B_dose","Regular")']
dict_of_dose_ranges = {
                        '("A_dose","Super")':[1,2,3],
                        '("A_dose","Light")':[40,70,90],
                        '("B_dose","Regular")':[130,200,500,700]}
</code></pre>

<p>Is there an alternate tool in python that can take tuples as inputs or a different way for me to trick it into working? </p>

<p>You can build a list of conditions and logically condense them with <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.all.html" rel="nofollow"><code>np.all</code></a> instead of using <code>query</code>:</p>

<pre><code>for dosage_comb in product(*dict_of_dose_ranges.values()):
    dosage_items = zip(dict_of_dose_ranges.keys(), dosage_comb)
    condition = np.all([dosage_df[col] == dose for col, dose in dosage_items], axis=0)
    sub_df = dosage_df[condition]
</code></pre>

<p>This method seems to be a bit more flexible than <code>query</code>, but when filtering across many columns I've found that <code>query</code> often performs better.  I don't know if this is true in general though.</p>

python pandas typeerror