<p>我认为你错用了<code>pivot</code>而不是<code>pivot_table</code></p>
<pre><code>data = [[np.NaN, 101, 331, 'Squirtle', 876],
[2021, 101, 1231, 'Squirtle', 200],
[np.NaN, 101, 331, 'Wartortle', 1000],
[2021, 101, 1231, 'Wartortle', 1200],
[np.NaN, 101, 331, 'Blastoise', 3100],
[2021, 101, 1231, 'Blastoise', 4200],
[2022, 101, 1231, 'Blastoise', 10000]]
df.pivot_table(index=['Year', 'Start', 'End'], columns='Name', values='Price')
</code></pre>
<p>产出:</p>
<pre><code>Name Blastoise Squirtle Wartortle
Year Start End
2021.0 101 1231 4200.0 200.0 1200.0
2022.0 101 1231 10000.0 NaN NaN
</code></pre>
<p>而如果用占位符值(如1000)替换这些值</p>
<pre><code>df = df.fillna(1000)
df.pivot_table(index=['Year', 'Start', 'End'], columns='Name', values='Price')
</code></pre>
<p>你会得到你想要的:</p>
<pre><code>Name Blastoise Squirtle Wartortle
Year Start End
1000.0 101 331 3100.0 876.0 1000.0
2021.0 101 1231 4200.0 200.0 1200.0
2022.0 101 1231 10000.0 NaN NaN
</code></pre>