How to reshape every nth row's data using pandas?

后端 未结 3 1636
余生分开走
余生分开走 2021-01-26 18:37

I need help in reshaping a data in csv file that have over 10000 row by 10 each. For example I have this csv file :

Ale Brick
1   ww
2   ee
3   qq
3   xx
5   dd
         


        
3条回答
  •  滥情空心
    2021-01-26 18:51

    You can group by every 10th row and then reshape the values

    df.groupby(np.repeat(np.arange(len(df) / 10), 10))['Brick'].apply(lambda x: x.values.reshape(2,5))
    
    0.0    [[ww, ee, qq, xx, dd], [gg, hh, tt, yy, uu]]
    1.0    [[ii, oo, pp, mm, ww], [zz, cc, rr, tt, ll]]
    

提交回复
热议问题