# How many ways can it be reshaped?

**URL:** <https://discourse.julialang.org/t/how-many-ways-can-it-be-reshaped/61530>\
**Category:** Data\
**Tags:** dataframes, reshaping\
**Created:** [May 20, 2021, 5:33pm UTC](https://discourse.julialang.org/t/how-many-ways-can-it-be-reshaped/61530 "2021-05-20T17:33:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [May 20, 2021, 5:33pm UTC](https://discourse.julialang.org/t/how-many-ways-can-it-be-reshaped/61530/1 "2021-05-20T17:33:42Z")

</div>

I have found a few ways to solve the following problem, but I’m sure there are many others.

```julia
X=["Product_id","Date","Unit","Revenue","Product_id","Date","Unit","Revenue","Product_id","Date","Unit","Revenue"]
Y=["1234","01-2014-20","1","4321","2345","01-2014-21","2","4321","3211","01-2014-22","3","12345"]
df=DataFrame(X=X,Y=Y)
grp=groupby(df,:X)

###sol. 1#####
names=unique(df.X)
cols=[c.Y for c in grp]
dict=[n=>v for (n,v) in zip(names,cols)]
dfd1=DataFrame(dict)

###sol. 2#####
D=[c.X[1]=>c.Y for c in grp]
dfd2=DataFrame(D)

```

I would like to see solutions that make use of named-tuples, or the unstack function, or any other function suitable for the purpose.

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [May 20, 2021, 7:32pm UTC](https://discourse.julialang.org/t/how-many-ways-can-it-be-reshaped/61530/2 "2021-05-20T19:32:12Z")

</div>

Here’s a few 🙂 (some of these produce additional columns but it’s easy to get rid of them if necessary):

```julia
vcat((permutedims(df[i:i+3, :], :X) for i in 1:4:nrow(df)-1)...)
vcat((permutedims(combine(grp, g->g[i,:]), :X) for i in 1:3)...)

# Using named tuples
DataFrame((; (Symbol.(df[i:i+3, :X]) .=> df[i:i+3, :Y])...) for i in 1:4:9)
DataFrame((; (Symbol(g[i, :X]) => g[i, :Y] for g in grp)...) for i in 1:3)

DataFrame([df[j+4i,2] for i in 0:2, j in 1:4], df[1:4, 1])

df.Z = repeat(1:3, inner=4)
unstack(df, :X, :Y)

```

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [May 21, 2021, 10:43am UTC](https://discourse.julialang.org/t/how-many-ways-can-it-be-reshaped/61530/3 "2021-05-21T10:43:57Z")

</div>

Just what I was looking for. Thank you very much @sijo.  
Could you explain me the rationale of the named tuple syntax?  
That is, where does it (; (keys. =\> values) …) come from

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [May 21, 2021, 3:24pm UTC](https://discourse.julialang.org/t/how-many-ways-can-it-be-reshaped/61530/4 "2021-05-21T15:24:43Z")

</div>

You asked for named tuples so I looked for a way to use them 🙂

The `DataFrame()` constructor accepts an iterator of named tuples (one per row). You can construct a named tuple with `(a=1, b=2)` or `(; a=1, b=2)` (names are literal) or with `(; :a => 1, :b => 2)` (names can be any expression that returns a symbol).

In your problem the names are not literal, they are computed, so I have to use that last version with pairs.
