# Transform DataFrame into a single row by adding row contents as new columns

**URL:** https://discourse.julialang.org/t/transform-dataframe-into-a-single-row-by-adding-row-contents-as-new-columns/60400
**Category:** New to Julia
**Tags:** dataframes
**Created:** [May 1, 2021, 8:35pm UTC](https://discourse.julialang.org/t/transform-dataframe-into-a-single-row-by-adding-row-contents-as-new-columns/60400 "2021-05-01T20:35:08Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![alessio](https://avatars.discourse-cdn.com/v4/letter/a/d6d6ee/32.png) [@alessio](https://discourse.julialang.org/u/alessio)
#### Post date: [May 1, 2021, 8:35pm UTC](https://discourse.julialang.org/t/transform-dataframe-into-a-single-row-by-adding-row-contents-as-new-columns/60400/1 "2021-05-01T20:35:08Z")

</div>

I would like to convert

```julia
df = DataFrame(A=['x','y','z','t'], B=[1,2,3,4])
4×2 DataFrame
 Row │ A B
     │ Char Int64
─────┼─────────────
   1 │ x 1
   2 │ y 2
   3 │ z 3
   4 │ t 4

```

to

```julia
8×1 DataFrame
 Row │ F1 F2 F3 F4 F5 F6 F7 F8
     │ Char Int64 Char Int64 Char Int64
─────┼─────────────────────────────────────────────────────────────
   1 │ x 1 y 2 z 3 t 4

```

Is there a simple way to apply this transformation?

---

<div class="post-metadata">

### Author: ![bashonubuntu](https://avatars.discourse-cdn.com/v4/letter/b/f19dbf/32.png) [@bashonubuntu](https://discourse.julialang.org/u/bashonubuntu)
#### Post date: [May 1, 2021, 9:12pm UTC](https://discourse.julialang.org/t/transform-dataframe-into-a-single-row-by-adding-row-contents-as-new-columns/60400/2 "2021-05-01T21:12:59Z")

</div>

```julia
julia> DataFrame(reshape(permutedims([df[:, 1] df[:, 2]]), 1, 8))

1×8 DataFrame
│ Row │ x1 │ x2 │ x3 │ x4 │ x5 │ x6 │ x7 │ x8 │
│ │ Any │ Any │ Any │ Any │ Any │ Any │ Any │ Any │
├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤
│ 1 │ 'x' │ 1 │ 'y' │ 2 │ 'z' │ 3 │ 't' │ 4 │

```

---

<div class="post-metadata">

### Author: ![stiven.roytman](https://avatars.discourse-cdn.com/v4/letter/s/d07c76/32.png) [@stiven.roytman](https://discourse.julialang.org/u/stiven.roytman)
#### Post date: [May 1, 2021, 9:14pm UTC](https://discourse.julialang.org/t/transform-dataframe-into-a-single-row-by-adding-row-contents-as-new-columns/60400/3 "2021-05-01T21:14:32Z")

</div>

I would do something like this:

```julia-repl
julia> hcat((eachrow(df) .|> DataFrame)..., makeunique=true)

 Row │ A B A_1 B_1 A_2 B_2 A_3 B_3   
     │ Char Int64 Char Int64 Char Int64 Char Int64 
─────┼────────────────────────────────────────────────────
   1 │ x 1 y 2 z 3 t 4

```

---

<div class="post-metadata">

### Author: ![alessio](https://avatars.discourse-cdn.com/v4/letter/a/d6d6ee/32.png) [@alessio](https://discourse.julialang.org/u/alessio)
#### Post date: [May 1, 2021, 9:31pm UTC](https://discourse.julialang.org/t/transform-dataframe-into-a-single-row-by-adding-row-contents-as-new-columns/60400/4 "2021-05-01T21:31:27Z")

</div>

I did this (Julia 1.6.1):

```julia
DataFrame(reshape(permutedims([df[:, 1] df[:, 2]]), 1, 8), :auto)

```
