7

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 row number. I would like to convert it to just one row.

So for example if I have

A,B,C,D,E
---------
1,2,3,4,5
6,7,8,9,10
11,12,13,14,5

I want as a result a dataframe with a single row:

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
--------------------------
1,2,3,4,5,6,7,8,9,10,11,12,13,14,5

What would be the most idiomatic way to do this in Pandas?

4 Answers 4

11

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
4
  • This is want @Wen posted right? other than incremeting the index Commented Dec 10, 2017 at 5:19
  • 1
    @Dark without the swaplevel :) Commented Dec 10, 2017 at 5:20
  • I missed that one, though I learnt column mapping from you, you get my vote ;) Commented Dec 10, 2017 at 5:21
  • nice for this , :-)
    – BENY
    Commented Dec 10, 2017 at 5:22
6

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)
5

We need stack and swaplevel

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

Or you can using numpy

pd.DataFrame(data=np.concatenate(df.values),index=[m+'_'+str(n) for m,n in zip(df.columns.tolist()*3,np.repeat([1,2,3],df.shape[1]))]).T
Out[551]: 
   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
2
  • I thought of unstack, you went for stack ; ) Commented Dec 10, 2017 at 5:06
  • 1
    @Dark aha, almost same , even we can melt :-)
    – BENY
    Commented Dec 10, 2017 at 5:07
2

Another way using list comprehension -

ndf = pd.DataFrame(df.values.reshape(1, -1)[0]).T
ndf.columns = [j + '_' + str(i) for i in range(1, 4) for j in df.columns]
1
  • Your answer is badly written. But, this is the only answer that has what I am looking for DataFrame().T. That .T is all what I needed to add in my case as I already had a flattened numpy vector array. Commented Jun 12, 2020 at 19:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.