<p>我认为在本例中,为了检查代码,最好至少显示3轮(假设您想要比仅检查2轮更一般的内容),因此我将扩展示例数据:</p>
<pre><code> Dan Mike Dave
0 Adrian Peterson Eddie Lacy Jamaal Charles
1 Calvin Johnson Leveon Bell Dez Bryant
2 Tom Brady Lionel Messi Lebron James
</code></pre>
<p>首先,使用<code>stack()</code>获得一个暂定的草稿顺序。你知道吗</p>
<pre><code>df2 = df.stack().reset_index().reset_index()
df2.columns = ['Order','Round','Drafter','Player']
Order Round Drafter Player
0 0 0 Dan Adrian Peterson
1 1 0 Mike Eddie Lacy
2 2 0 Dave Jamaal Charles
3 3 1 Dan Calvin Johnson
4 4 1 Mike Leveon Bell
5 5 1 Dave Dez Bryant
6 6 2 Dan Tom Brady
7 7 2 Mike Lionel Messi
8 8 2 Dave Lebron James
</code></pre>
<p>请注意,第0轮和第2轮的顺序是正确的,而不是第1轮。确定第1轮的顺序的一个相当简单的方法是乘以负1(更具体地说是所有不可被2整除的轮),然后排序:</p>
<pre><code>df2['Order'] = np.where( df2['Round'].mod(2)==1, -df2['Order'], df2['Order'] )
df3 = df2.sort(['Round','Order']).reset_index(drop=True)
Order Round Drafter Player
0 0 0 Dan Adrian Peterson
1 1 0 Mike Eddie Lacy
2 2 0 Dave Jamaal Charles
3 -5 1 Dave Dez Bryant
4 -4 1 Mike Leveon Bell
5 -3 1 Dan Calvin Johnson
6 6 2 Dan Tom Brady
7 7 2 Mike Lionel Messi
8 8 2 Dave Lebron James
</code></pre>
<p>此时,索引正确地保存了草稿顺序,您可以删除order列(我只是把它留在那里,以便更清楚地了解代码的工作方式)。你知道吗</p>