# Creating dataframe with arrays of different length

**URL:** <https://discourse.julialang.org/t/creating-dataframe-with-arrays-of-different-length/71642>\
**Category:** New to Julia\
**Tags:** dataframes\
**Created:** [November 17, 2021, 9:50am UTC](https://discourse.julialang.org/t/creating-dataframe-with-arrays-of-different-length/71642 "2021-11-17T09:50:44Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![jnewbie](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@jnewbie](https://discourse.julialang.org/u/jnewbie)\
**Post date:** [November 17, 2021, 9:50am UTC](https://discourse.julialang.org/t/creating-dataframe-with-arrays-of-different-length/71642/1 "2021-11-17T09:50:44Z")

</div>

Hi,

I have 3 arrays with which I´d like to make a dataframe where each column corresponds to an array ( column 1 = abc, column 2 = def, column 3 = ghi) and the column names to the names of the arrays. I tried `hcat` but I get an error because my arrays don`t have the same length.

> abc = [6,7,8,9,10]  
> def = [“a”,“b”,“c”]  
> ghi = [87]

Does anyone have any ideas ?

Thank you,

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [November 17, 2021, 10:00am UTC](https://discourse.julialang.org/t/creating-dataframe-with-arrays-of-different-length/71642/2 "2021-11-17T10:00:40Z")

</div>

The DataFrames.jl documentation is really good, please read it when you find some time.

This should work fine:

```julia
DataFrame(abc=abc, def=def, ghi=ghi)

```

---

<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:** [November 17, 2021, 10:32am UTC](https://discourse.julialang.org/t/creating-dataframe-with-arrays-of-different-length/71642/3 "2021-11-17T10:32:29Z")

</div>

That won’t work, as OP said this will raise an error:

```julia
julia> DataFrame(abc = abc, def = def)
ERROR: DimensionMismatch("column :abc has length 5 and column :def has length 3")

```

This is just how DataFrames works - columns have to be the same length. You need to ask yourself:

1. Is a DataFrame the right structure for my data? Why do I want to use a DataFrame if the columns aren’t the same length?

2. If the answer to (1) is yes I really want a DataFrame, what should be stored in shorter columns in those rows for which information in the longer columns is available? To make it concrete, your table looks like this:

| abc | def | ghi |
| --- | --- | --- |
| 6 | “a” | 87 |
| 7 | “b” | ? |
| 8 | “c” | ? |
| 9 | ? | ? |

What’s in those question marks? If there’s no natural correspondence between the value of `abc` and `ghi` in a given row, then maybe a DataFrame just isn’t the right storage format for your data.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [November 17, 2021, 10:39am UTC](https://discourse.julialang.org/t/creating-dataframe-with-arrays-of-different-length/71642/4 "2021-11-17T10:39:38Z")

</div>

Thank you @nilshg I did read the first part of his question and skipped the details. Didn’t notice the columns of different size.

@jnewbie what would you expect as the result when columns have different size?

---

<div class="post-metadata">

**Author:** ![jnewbie](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@jnewbie](https://discourse.julialang.org/u/jnewbie)\
**Post date:** [November 17, 2021, 11:29am UTC](https://discourse.julialang.org/t/creating-dataframe-with-arrays-of-different-length/71642/5 "2021-11-17T11:29:20Z")

</div>

The ? could be replaced “missing”. I need to make that DataFrame so it can be exported as a CSV file for a purpose outside of Julia.

---

<div class="post-metadata">

**Author:** ![jnewbie](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@jnewbie](https://discourse.julialang.org/u/jnewbie)\
**Post date:** [November 17, 2021, 11:49am UTC](https://discourse.julialang.org/t/creating-dataframe-with-arrays-of-different-length/71642/6 "2021-11-17T11:49:01Z")

</div>

| abc | def | ghi |
| --- | --- | --- |
| 6 | “a” | 87 |
| 7 | “b” | mssing |
| 8 | “c” | missing |
| 9 | missing | missing |

I would like to get this if it is possible.

---

<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:** [November 17, 2021, 11:56am UTC](https://discourse.julialang.org/t/creating-dataframe-with-arrays-of-different-length/71642/7 "2021-11-17T11:56:44Z")

</div>

You could do:

```julia
julia> DataFrame(abc = abc,
                 def = [def; fill(missing, df_length-length(def))],
                 ghi = [ghi; fill(missing, df_length-length(ghi))])
5×3 DataFrame
 Row │ abc def ghi     
     │ Int64 String? Int64?  
─────┼─────────────────────────
   1 │ 6 a 87
   2 │ 7 b missing 
   3 │ 8 c missing 
   4 │ 9 missing missing 
   5 │ 10 missing missing 

```

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [November 17, 2021, 12:26pm UTC](https://discourse.julialang.org/t/creating-dataframe-with-arrays-of-different-length/71642/8 "2021-11-17T12:26:53Z")

</div>

With

```julia
df_length=maximum(length.([abc,def,ghi]))

```

for example 😉

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [November 17, 2021, 12:28pm UTC](https://discourse.julialang.org/t/creating-dataframe-with-arrays-of-different-length/71642/9 "2021-11-17T12:28:49Z")

</div>

```julia
DataFrame(abc = [abc; fill(missing, df_length-length(abc))],
                        def = [def; fill(missing, df_length-length(def))],
                        ghi = [ghi; fill(missing, df_length-length(ghi))])

```

is a bit more generic, if `abc` isn’t the longest one.  
But it’s a great one-liner!  
I don’t show you mine ☹

---

<div class="post-metadata">

**Author:** ![jnewbie](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@jnewbie](https://discourse.julialang.org/u/jnewbie)\
**Post date:** [November 17, 2021, 12:29pm UTC](https://discourse.julialang.org/t/creating-dataframe-with-arrays-of-different-length/71642/10 "2021-11-17T12:29:05Z")

</div>

it works, thanks !

---

<div class="post-metadata">

**Author:** ![jnewbie](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@jnewbie](https://discourse.julialang.org/u/jnewbie)\
**Post date:** [November 17, 2021, 12:32pm UTC](https://discourse.julialang.org/t/creating-dataframe-with-arrays-of-different-length/71642/11 "2021-11-17T12:32:19Z")

</div>

Thanks !

---

<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:** [November 17, 2021, 1:15pm UTC](https://discourse.julialang.org/t/creating-dataframe-with-arrays-of-different-length/71642/12 "2021-11-17T13:15:50Z")

</div>

Another option using LazyStack.jl:

```julia
DataFrame(rstack(abc, def, ghi, fill=missing), [:abc, :def, :ghi])

```

---

<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:** [November 17, 2021, 2:02pm UTC](https://discourse.julialang.org/t/creating-dataframe-with-arrays-of-different-length/71642/13 "2021-11-17T14:02:46Z")

</div>

Another option using PaddedViews.jl:

```julia
n = maximum(length.((abc, def, ghi)))
abc, def, ghi = PaddedView.(missing, (abc,def,ghi), Ref((n,)), Ref((1,)))
df = DataFrame(abc=abc, def=def, ghi=ghi)

```
