Populate a Dataframe from an array

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

struct Region

where res_tech is another struct that is:

struct res_tech

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

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:

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)

However I get this error:

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

Can you help me out with this issue?

Thanks a lot!

what is the length of



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

The results from that operation is a 24x1 Array{Float64,2}

It’s hard to be certain given that your post is not an MWE (see here: Please read: make it easier to help you), 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:

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

or something like that at least…

1 Like

Aside from what @nilshg wrote (please read the DataFrames Documentation!), 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.