擅长:python、mysql、java
<p>我相信,如果您在多索引中包含<code>item</code>,那么您可以取消返回:</p>
<pre><code>df.set_index(['year', 'month', 'item']).unstack(level=-1)
</code></pre>
<p>这将产生:</p>
<pre class="lang-none prettyprint-override"><code> value
item item 1 item 2
year month
2004 1 21 277
2 43 244
3 12 262
4 80 201
5 22 287
6 52 284
7 90 249
8 14 229
9 52 205
10 76 207
11 88 259
12 90 200
</code></pre>
<p>它比使用<code>pivot_table</code>快一点,与使用<code>groupby</code>的速度差不多或稍慢一点。</p>