小编JBS*_*BSH的帖子

按 id 上的排名透视 Pandas 数据框

我目前正在尝试通过 'rank' 上的 'id' 来旋转我的 Pandas DataFrame

print(df)

     id  rank  year  
0   key0  1    2011  
1   key0  2    2012  
2   key0  3    2013  
3   key1  1    2014  
4   key1  2    2015  
5   key1  3    2016  
6   key2  1    2017 
7   key2  2    2018 
8   key2  3    2019 
Run Code Online (Sandbox Code Playgroud)

根据 max('rank'),我想创建尽可能多的 'years' 列并根据升序赋予它们值

print(df)

     id  rank1  year1  rank2  year2  rank3   year3  
0   key0   1     2011    2     2012    3      2013
1   key1   1     2014    2     2015    3      2016  
2   key2   1     2017    2 …
Run Code Online (Sandbox Code Playgroud)

python pivot python-3.x pandas

6
推荐指数
1
解决办法
193
查看次数

标签 统计

pandas ×1

pivot ×1

python ×1

python-3.x ×1