python 重塑数据框

python reshaping a dataframe

我的 datafram2 中有一堆行,看起来像这样。很多行有 2 列。

player         pos

Blake Bortles   QB
Alvin Kamara    RB
Dion Lewis      RB
Keenan Allen    WR
Michael Thomas  WR
Tyrell Williams WR
Hunter Henry    TE
Stephen Gostkowski  K
Los Angeles Chargers    D
Totals       Team 1.0
Blake Bortles   QB
Alvin Kamara    RB
Dion Lewis  RB
Keenan Allen    WR
Michael Thomas  WR
Tyrell Williams WR
Hunter Henry    TE
Stephen Gostkowski  K
Jacksonville Jaguars    D
Totals  Team 2.0
Blake Bortles   QB
Todd Gurley RB
Alvin Kamara    RB
Michael Thomas  WR
Cooper Kupp WR
Tyrell Williams WR
Hunter Henry    TE
Stephen Gostkowski  K
Los Angeles Chargers    D

我正在尝试重塑它,以便每 11 行开始一个新行,pos 值是列名。所以我希望它看起来像这样:

 QB                 RB               RB                 WR               WR                     WR                   TE                      K                D                team
 Blake Bortles      Alvin Kamara     Dion Lewis     Keenan Allen      Michael Thomas     Tyrell Williams     Hunter Henry      Stephen Gostkowski     Los Angeles Chargers

Totals Team 1.0 的最后一列并不重要。但我希望第 2 排成为下一组玩家。我试过重塑。

如果我删除 pos 列并执行以下操作,我已经设法做到了:

test = pd.DataFrame(rearrange.values.reshape(-1, 10), 
                columns=['QB','RB','RB','WR','WR','WR','TE','K','D','Team'])

但是我如何使用那里的 pos 列呢?

有什么想法吗?

如果每个组中 pos 的顺序和值都相同,您似乎需要:

df = pd.DataFrame(df['player'].values.reshape(-1, 10), 
                columns=df['pos'].iloc[:9].tolist() + ['total'])
print (df)
              QB            RB            RB              WR              WR  \
0  Blake Bortles  Alvin Kamara    Dion Lewis    Keenan Allen  Michael Thomas   
1  Blake Bortles  Alvin Kamara    Dion Lewis    Keenan Allen  Michael Thomas   
2  Blake Bortles   Todd Gurley  Alvin Kamara  Michael Thomas     Cooper Kupp   

                WR            TE                   K                     D  \
0  Tyrell Williams  Hunter Henry  Stephen Gostkowski  Los Angeles Chargers   
1  Tyrell Williams  Hunter Henry  Stephen Gostkowski  Jacksonville Jaguars   
2  Tyrell Williams  Hunter Henry  Stephen Gostkowski  Los Angeles Chargers   

         total  
0  Totals Team  
1  Totals Team  
2  Totals Team