Pandas: query string where column name contains special characters
<p>I am working with a data frame that has a structure something like the following:</p>

<pre><code>In[75]: df.head(2)
Out[75]: 
  statusdata             participant_id association  latency response  \
0   complete  CLIENT-TEST-1476362617727       seeya      715  dislike   
1   complete  CLIENT-TEST-1476362617727      welome      800     like   

   stimuli elementdata statusmetadata demo$gender  demo$question2  \
0  Sample B    semi_imp       complete        male              23   
1  Sample C    semi_imp       complete      female              23   
</code></pre>

<p>I want to be able to run a query string against the column <code>demo$gender</code>.</p>

<p>I.e,</p>

<pre><code>df.query("demo$gender=='male'")
</code></pre>

<p>But this has a problem with the <code>$</code> sign. If I replace the <code>$</code> sign with another delimited (like <code>-</code>) then the problem persists. Can I fix up my query string to avoid this problem. I would prefer not to rename the columns as these correspond tightly with other parts of my application.</p>

<p>I really want to stick with a query string as it is supplied by another component of our tech stack and creating a parser would be a heavy lift for what seems like a simple problem.</p>

<p>Thanks in advance.</p>

<p>The current implementation of <code>query</code> requires the string to be a valid python expression, so column names must be valid python identifiers.  Your two options are renaming the column, or using a plain boolean filter, like this:</p>

<pre><code>df[df['demo$gender'] =='male']
</code></pre>

python pandas dataframe