Convert a Pandas DataFrame into a single row DataFrame

前端 未结 4 1973
情深已故
情深已故 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:16

    Unstack and map i.e

    ndf = df.unstack().to_frame().T
    
    ndf.columns = ndf.columns.map('{0[0]}_{0[1]}'.format) 
    
        A_0  A_1  A_2  B_0  B_1  B_2  C_0  C_1  C_2  D_0  D_1  D_2  E_0  E_1  E_2
    0    1    6   11    2    7   12    3    8   13    4    9   14    5   10    5
    

    In case you want the sorted columns then you can do

    ndf = df.unstack().to_frame().T.sort_index(1,1)
    

提交回复
热议问题