Populate a Dataframe from an array

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)
end

or something like that at least…

1 Like