# \[DataFrames Question\]: Transpose of dataframe

**URL:** <https://discourse.julialang.org/t/dataframes-question-transpose-of-dataframe/28524>\
**Category:** Data\
**Tags:** question\
**Created:** [September 8, 2019, 12:42am UTC](https://discourse.julialang.org/t/dataframes-question-transpose-of-dataframe/28524 "2019-09-08T00:42:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Patrick\_Mineault](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/patrick_mineault/32/10168_2.png) [@Patrick\_Mineault](https://discourse.julialang.org/u/Patrick_Mineault)\
**Post date:** [September 8, 2019, 12:42am UTC](https://discourse.julialang.org/t/dataframes-question-transpose-of-dataframe/28524/1 "2019-09-08T00:42:17Z")

</div>

# Question: How do I transpose a dataframe (columns become rows, rows become columns)?

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [September 8, 2019, 2:03am UTC](https://discourse.julialang.org/t/dataframes-question-transpose-of-dataframe/28524/2 "2019-09-08T02:03:54Z")

</div>

There isn’t a way to do this directly. You can convert to a matrix and transpose that, but you need to deal with the column in df1 that you want to be heaters in df2, and you need to turn the headers into a column. Or do something like this (untested, and assumes your first column should become the new headers):

```julia
let df2 = DataFrame(old_headers=names(df)[2:end])
    for row in eachrow(df1)
        df2[!, Symbol(row[1])] = vec(row[2:end])
    end
    df2
end

```

Alternatively, something creative with pivoting should work - [see the docs](https://juliadata.github.io/DataFrames.jl/stable/man/reshaping_and_pivoting/)

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [June 2, 2020, 2:41am UTC](https://discourse.julialang.org/t/dataframes-question-transpose-of-dataframe/28524/3 "2020-06-02T02:41:27Z")

</div>

For people that find this thread searching for a way to solve this problem without having to write their own ad hoc solution, there is [a solution using DataFrames.jl methods `stack` and `unstack`](https://stackoverflow.com/questions/37668312/transpose-of-julia-dataframe).
