Reshape dataframe to dataframe with unlimited rows and filling zeroes where no values

前端 未结 2 1183
耶瑟儿~
耶瑟儿~ 2020-12-20 02:02

Is there a way to reshape DataFrame to another with unrestricted rows. I just want a DataFrame with 3 columns, no matter how many rows is going to be in DataFrame?

F

2条回答
  •  死守一世寂寞
    2020-12-20 02:14

    Use:

    df = (pd.DataFrame(letters.groupby(letters.index // 3)['Letters']
                              .apply(list)
                              .values
                              .tolist(), columns=['first','second','third']).fillna(0))
    
    print (df)
      first second third
    0     A      B     C
    1     D      E     F
    2     G      H     I
    3     J      0     0
    

提交回复
热议问题