# Populate a Dataframe from an array

**URL:** <https://discourse.julialang.org/t/populate-a-dataframe-from-an-array/35526>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [March 4, 2020, 3:21pm UTC](https://discourse.julialang.org/t/populate-a-dataframe-from-an-array/35526 "2020-03-04T15:21:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luigi\_Bottecchia](https://avatars.discourse-cdn.com/v4/letter/l/cdc98d/32.png) [@Luigi\_Bottecchia](https://discourse.julialang.org/u/Luigi_Bottecchia)\
**Post date:** [March 4, 2020, 3:21pm UTC](https://discourse.julialang.org/t/populate-a-dataframe-from-an-array/35526/1 "2020-03-04T15:21:24Z")

</div>

Hi all! I’m learning Julia and these are my first steps. I have a struct that looks like this:

```julia
struct Region
    elements::Array{res_tech}
    demand::Matrix{Float64}
end

```

where res\_tech is another struct that is:

```julia
struct res_tech
    priority::Int
    resource::Matrix{Float64}
    installed_capacity::Int
    name::String
end

```

Now, I have created a struct based on region that is

```julia
Node = Region([wind,pv],el_dem)

```

where wind and pv are 2 res\_tech. I want to create a dataframe (let’s call it electricity) that has as column name the res\_tech.name and it’s filled the resulting from a function that multiplies res\_tech.resource by res\_tech.installed\_capacity.

I tried several things and this is the last one:

```julia
electricity = DataFrame()

for i in eachindex(Node.elements)
    col_name = Node.elements[i].name
    electricity.col_name = max_production(Node.elements[i].resource,Node.elements[i].installed_capacity)
end

```

However I get this error:

```julia
DimensionMismatch("Array could not be broadcast to match destination")

```

Can you help me out with this issue?

Thanks a lot!  
Luigi

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [March 4, 2020, 3:23pm UTC](https://discourse.julialang.org/t/populate-a-dataframe-from-an-array/35526/2 "2020-03-04T15:23:49Z")

</div>

what is the length of

```julia
max_production(Node.elements[i].resource,Node.elements[i].installed_capacity

```

?

The error suggests that the lengths of the arrays returned by indexing the object in that way are not consistent.

---

<div class="post-metadata">

**Author:** ![Luigi\_Bottecchia](https://avatars.discourse-cdn.com/v4/letter/l/cdc98d/32.png) [@Luigi\_Bottecchia](https://discourse.julialang.org/u/Luigi_Bottecchia)\
**Post date:** [March 4, 2020, 3:26pm UTC](https://discourse.julialang.org/t/populate-a-dataframe-from-an-array/35526/3 "2020-03-04T15:26:01Z")

</div>

Hi@pdeffebach!  
The results from that operation is a 24x1 Array{Float64,2}

---

<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:** [March 4, 2020, 3:29pm UTC](https://discourse.julialang.org/t/populate-a-dataframe-from-an-array/35526/4 "2020-03-04T15:29:13Z")

</div>

It’s hard to be certain given that your post is not an MWE (see here: [Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757)), but there are a few potential issues.

First of all, you seem to be trying to use dot-notation indexing (`electricity.col_name`) within a loop and using a `String` object (`Node.elements[i].name` seems to be a string based on your struct definition). This won’t work, you need to index the dataframe with a Symbol using `electricity[:, Symbol(col_name)]`. However it seems that the column doesn’t even exist in your DataFrame so you’ll have to create it first.

Also note that `Node.elements` as an array is iterable by itself, no need to use indices:

```julia
for el in Node.elements
    col_name = Symbol(el.name)
    electricity[!, col_name] = max_production(el.resource, el.installed_capacity)
end

```

or something like that at least…

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [March 4, 2020, 3:36pm UTC](https://discourse.julialang.org/t/populate-a-dataframe-from-an-array/35526/5 "2020-03-04T15:36:03Z")

</div>

Aside from what @nilshg wrote (please read the [DataFrames Documentation](https://pkg.julialang.org/docs/DataFrames/AR9oZ/0.20.2/)!), your problem is that your object is a `Matrix`, not a `Vector`. Note the `Array{Float64, 2}` instead of `Array{Float64, 1}`.

You can call `vec` on your object to collect it into a `Vector`. _But_ I think you still have the wrong number of elements. So you have two problems

1. Your array is a matrix, not a vector
2. The length of your array (the N in N x 1) is not the correct length.
