# Matrix to dataframe conversion with structure of existing dataframe

**URL:** <https://discourse.julialang.org/t/matrix-to-dataframe-conversion-with-structure-of-existing-dataframe/129278>\
**Category:** Data\
**Tags:** dataframes, convert\
**Created:** [May 23, 2025, 2:03pm UTC](https://discourse.julialang.org/t/matrix-to-dataframe-conversion-with-structure-of-existing-dataframe/129278 "2025-05-23T14:03:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [May 23, 2025, 2:03pm UTC](https://discourse.julialang.org/t/matrix-to-dataframe-conversion-with-structure-of-existing-dataframe/129278/1 "2025-05-23T14:03:41Z")

</div>

Is there any built-in function to convert a matrix to a DataFrame with the structure of a second DataFrame ?

i.e. :

```julia
df = DataFrame(a = [1,2,3], b= ["a","b","c"])

# Option 1, types not promoted
dfnames = names(df)
m = [4 "d"; 5 "e"]
df2 = DataFrame(m,dfnames) # doesn't promote types

# Option 2, manual assignment
df2 = similar(df,size(m,1))
[df2[:,cidx] = m[:,cidx] for cidx in axes(df2,2)]
df2 # fine

```

I have scrolled through the doc for the DataFrame constructor and didn’t find anything.. it would be nice to have something like:

```julia
df = DataFrame(matrix,otherdf)

```

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [May 23, 2025, 3:52pm UTC](https://discourse.julialang.org/t/matrix-to-dataframe-conversion-with-structure-of-existing-dataframe/129278/2 "2025-05-23T15:52:21Z")

</div>

Seems a pretty niche use case, and fundamentally the matrix just doesn’t store the type information you’re after, but if if you just want to save some typing you can do

```julia
julia> df2 = identity.(DataFrame(m,dfnames))
2×2 DataFrame
 Row │ a b      
     │ Int64 String 
─────┼───────────────
   1 │ 4 d
   2 │ 5 e

```

---

<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 23, 2025, 5:51pm UTC](https://discourse.julialang.org/t/matrix-to-dataframe-conversion-with-structure-of-existing-dataframe/129278/3 "2025-05-23T17:51:44Z")

</div>

```julia
df2 = similar(df, size(m,1))
df2 .= m

#or
df2 = similar(df, size(m,1)).=m

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [May 23, 2025, 7:06pm UTC](https://discourse.julialang.org/t/matrix-to-dataframe-conversion-with-structure-of-existing-dataframe/129278/4 "2025-05-23T19:06:03Z")

</div>

Another way:

```julia
df2 = copy(df[axes(m)...]) .= m

```

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [May 28, 2025, 8:51am UTC](https://discourse.julialang.org/t/matrix-to-dataframe-conversion-with-structure-of-existing-dataframe/129278/5 "2025-05-28T08:51:46Z")

</div>

All answers are valid, but I still believe a DF constructor that explicitly differentiate between data and structure (that goes behind the column names) would be welcomed…
