擅长:python、mysql、java
<p>此解决方案通过<code>iterrows</code>遍历数据帧中的每一行,如果该行是奇数行<code>[::(-1 if n % 2 else 1)]</code>,则反转该行,然后将结果与通过<code>df.columns.tolist() * df.shape[0]</code>按数据帧中的行数重复的起草者列表连接在一起。播放器和起草者通过<code>zip</code>函数连接,然后传递给数据帧构造函数。你知道吗</p>
<pre><code>draft = pd.DataFrame(zip(pd.concat([pd.Series(row.values)[::(-1 if n % 2 else 1)]
for n, row in df.iterrows()],
ignore_index=True),
df.columns.tolist() * df.shape[0]),
columns=['Player', 'Drafter'])
draft['round'] = 1 + draft.index.values // df.shape[1]
>>> draft
Player Drafter round
0 Adrian Peterson Dan 1
1 Eddie Lacy Mike 1
2 Jamaal Charles Dave 1
3 Dez Bryant Dan 2
4 Leveon Bell Mike 2
5 Calvin Johnson Dave 2
6 Tom Brady Dan 3
7 Lionel Messi Mike 3
8 Lebron James Dave 3
</code></pre>