擅长:python、mysql、java
<p>您的解决方案应该有一点改变-使用<code>set_index</code>中列的顺序,省略第二个<code>swaplevel</code>并添加{a1}:</p>
<pre><code>df = (df.set_index(['timestamp', 'source', 'ticker'])
.unstack(level=[1,2])
.swaplevel(0,2,axis=1)
.sort_index(axis=1)
)
print (df)
ticker A B
source LSE LSE
PX_CLOSE PX_LAST PX_OPEN PX_CLOSE PX_LAST PX_OPEN
timestamp
20180101 3 1 2 9 7 8
20180102 6 4 5 12 10 11
</code></pre>