且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将Pandas DataFrame转换为单行DataFrame

更新时间:2021-12-09 18:16:21

让我们尝试使用stackto_frame和T:

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

输出:

   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