How to transpose a DataFrame?

For example, if I have a DataFrame like the below. How do I transpose it?

df = 12×5 DataFrame
 Row │ Variable  Col1        Col2          Col3         Col4
         │ String    Float64     Float64     Float64     Float64
───┼──────────────────────────────────
   1   │ Var1         8.19625     7.74408     7.57055     7.42672
   2   │ Var2        12.5282     11.8329     11.5725     11.3412
   3   │ Var3          20.3213     20.2322     20.2025     20.1582
   4   │ Var4        18.6746     20.2428     20.8298     21.6727
   5   │ Var5       15.1324     16.456      16.9414     17.5668
   6   │ Var6        521.503     492.1       481.128     471.208

I tried the below and it did not work:
df_use = DataFrame(Matrix(df)');

Many thanks.

Try permutedims(df, 1)

1 Like

It works!

Thank you very much.

Fyi, there is a nice solution posted in SO to transpose dataframes, including the column names.

1 Like