擅长:python、mysql、java
<pre><code>pd.eval('df["Col 1"] + df["Col 2"]')
</code></pre>
<p>这使eval的参数保持为字符串,但不如在列名中没有空格的示例干净</p>
<p>示例:</p>
^{pr2}$
<p><strong>编辑</strong></p>
<p>经过一番调查,如果您使用的是python引擎,那么上面的方法在python 2.7或3.6中都能正常工作:</p>
<pre><code>pd.eval('df["Col 1"] + df["Col 2"]', engine='python')
</code></pre>
<p>但是,这并没有给您提供<code>numexpr</code>引擎所能提供的性能优势。在Python2.7中,此方法有效:</p>
<pre><code>pd.eval('df["Col 1"] + df["Col 2"]', engine='numexpr')
</code></pre>
<p>但是在Python3.6中,您会得到错误<code>ValueError: unknown type str160</code>。在</p>
<p>我的猜测是,这是因为pandas在3.6中将unicode字符串传递给<code>numexpr</code>,而在2.7中传递的是bytestring。我猜这个问题与<a href="https://github.com/pydata/numexpr/issues/142" rel="nofollow noreferrer">this issue</a>有关,可能也与{a2}有关。在</p>