Convert a Pandas DataFrame into a single row DataFrame

前端 未结 4 1976
情深已故
情深已故 2021-01-13 08:52

I\'ve seen similar questions but mine is more direct and abstract.

I have a dataframe with \"n\" rows, being \"n\" a small number.We can assume the index is just the

4条回答
  •  鱼传尺愫
    2021-01-13 09:24

    Let's try this, using stack, to_frame, and T:

    df.index = df.index + 1
    df_out = df.stack()
    df_out.index = df_out.index.map('{0[1]}_{0[0]}'.format)
    df_out.to_frame().T
    

    Output:

       A_1  B_1  C_1  D_1  E_1  A_2  B_2  C_2  D_2  E_2  A_3  B_3  C_3  D_3  E_3
    0    1    2    3    4    5    6    7    8    9   10   11   12   13   14    5
    

提交回复
热议问题