擅长:python、mysql、java
<p>您可以使用<code>as_index=False</code>来保留整数索引。您只需要一行代码就可以完成您需要的操作:</p>
<pre><code>vardataframe = voldataframe.groupby('Symbol', as_index=False).var()
</code></pre>
<p>代码中有几点:</p>
^{pr2}$
<p>将<code>ndewindex</code>设置为索引,但返回一个未使用的新数据帧。如果需要,可以执行<code>vardataframe.set_index(['newindex'], inplace=True)</code>。在</p>
<pre><code>vardataframe.ix[:,['newindex','Symbol','volatility']]
</code></pre>
<p>给出所有<code>NaN</code>的列<code>Symbol</code>,因为<code>Symbol</code>不是{<cd7>}的列,而只存在于其<code>index</code>中。使用<code>ix</code>查询不存在的列将得到所有<code>NaN</code>。正如@user2600939所提到的,您可以做<code>vardataframe.reset_index(inplace=True)</code>(或<code>vardataframe=vardataframe.reset_index()</code>)将<code>Symbol</code>放回一列。在</p>