擅长:python、mysql、java
<p>您可以通过<a href="https://pandas.pydata.org/pandas-docs/version/0.23.4/generated/pandas.Categorical.html" rel="nofollow noreferrer">^{<cd1>}</a>实现这一点:</p>
<pre><code>df.variable=pd.Categorical(df.variable,categories=df.variable.unique(),ordered=True)
df.pivot_table(index='date',columns='variable',values='value')
</code></pre>
<hr/>
<pre><code>variable A C B
date
2017 1 1 2
2018 1 1 2
</code></pre>
<p>这将顺序设置为<code>df.variable.unique()</code>,即<code>[A, C, B]</code></p>