1

I have 100 csv files each of which when read by pandas dataframe is like below:

gyrus GW WM
FlRt 1.2 1.0
FlLt 1.4 1.0
TlRt 1.3 1.1
TlLt 1.4 1.2

I need to convert programatically this to the form as below

FlRt GM FlRt WM FlLt GM FlLt WM TlRt GW TlRt WM TlLt GM TlLt WM
1.2. 1.0. 1.4. 1.0. 1.3 1.1 1.4. 1.2.

so that I can merge all the 100 files to form a large single dataframe

Thanks in advance

dratoms
  • 159
  • 11

1 Answers1

2

Pivot your dataframe:

out = df.set_index('gyrus').stack().to_frame().T
out.columns = out.columns.to_flat_index().str.join(' ')

# Output
print(out)
   FlRt GW  FlRt WM  FlLt GW  FlLt WM  TlRt GW  TlRt WM  TlLt GW  TlLt WM
0      1.2      1.0      1.4      1.0      1.3      1.1      1.4      1.2
Corralien
  • 109,409
  • 8
  • 28
  • 52