擅长:python、mysql、java
<p>您可以使用<code>ordered Categorical</code>:</p>
<pre><code>df['i2'] = pd.Categorical(df.i2, categories=['f', 'a', 'w', 'h'], ordered=True)
df.set_index(['i1','i2'],inplace=True)
print (df)
print (df.unstack('i2'))
c1 c2 \
i2 f a w h f a w
i1
x -0.663218 1.005395 0.236088 1.416896 2.729855 0.141692 0.136700
y 0.509393 0.370418 -1.301840 -1.067212 0.945016 -0.617570 1.377235
i2 h
i1
x -0.029020
y -2.346038
</code></pre>