python重塑数据框

chitown88

我的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放到do上,我已经做到了:

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

但是,该如何处理pos那里专栏呢?

任何的想法?

耶斯列尔

似乎您需要pos每个组中的order和value是否相同:

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  

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章