# Initializing a dataframe

**URL:** <https://discourse.julialang.org/t/initializing-a-dataframe/35933>\
**Category:** New to Julia\
**Created:** [March 13, 2020, 4:22pm UTC](https://discourse.julialang.org/t/initializing-a-dataframe/35933 "2020-03-13T16:22:30Z")\
**Posts on this page:** 4\
**Page:** 2

<div class="post-metadata">

**Author:** ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)\
**Post date:** [March 15, 2020, 12:11am UTC](https://discourse.julialang.org/t/initializing-a-dataframe/35933/21 "2020-03-15T00:11:32Z")

</div>

Here are some further experiments, getting closer to what I really want. At this point, it is no longer about my original problem, but about me being obsessed with getting a specific result.

```julia
struct A end
struct B end

function getCurrents(::A)
           random = rand()
           println("A")
           J_δ = [8, 6]
           J_β = [3,23]
           return (J_δ = J_δ, J_β = J_β, c = random)
end

function getCurrents(::B)
           dict = Dict()
           random = rand()
           dict[:J_δ] = [8, 6]
           dict[:J_β] = [3,25]
           return dict
end

a = A(); output_named_tuple = getCurrents(a)
for (k,v) in zip(keys(output_named_tuple), output_named_tuple)
  # eval evaluates in the global context
  eval(:($k = $v))
end

b = B(); dict = getCurrents(b)

for (k,v) in zip(keys(dict), dict)
  # eval evaluates in the global context
  eval(:($k = $v)) # Defines J_β, J_δ
end

```

My function getCurrents, either returns a NamedTuple or a Dictionary. When it returns a dictionary, J\_\alpha and J\_\beta only appear once. In the original solution, J\_\beta and J\_\delta appear each three times, which violates the DRY principle. Lower down, I have two loops, one over the NamedTuple, and one over the dictionary. In both cases, the variables J\_\beta and J\_\delta are instantiated in the global space (perhaps the wrong term in Julia) in the sense that in any function I could type something like

```julia
  z = J_\alpha + J_\beta

```

and I would get a result.

My real objective is to specify the variables I wish to track only ONCE, and have my callback collect the value of these variables once per time step of an ODE solver, and store the results in a DataFrame. Of course it is possible. Anything is possible: after all, that is what packages and modules are all about. Creating easy to use functionality that is not simple with the current structure of julia and packaging it for the user.

I am open to any suggestions you might have. This little excursion has taught me about NamedTuples, Structures, zip, loops of various kinds and the most basic form of metaprogramming.

One question: I wonder how efficient or non-efficient my approach is. Note that efficiency is not the point here, but feasibility.

When using DifferentialEquations.jl, if I must unpack a dictionary everything the right-hand side is invokedm there might be a penalty. Those are experiments I might drive myself to run.

Another issue: in the right-hand side routine, without the callback, J\_\beta and J\_\delta are variables local to the method. In the current approach, J\_\alpha and J\_\beta are defined in the global space, which is never a good idea. So one question to answer is whether it is possible to apply a macro to create a variable in a local context of some kind.

I have read the following three links: ’

> <https://stackoverflow.com/questions/21267962/how-can-i-eval-a-local-variable-in-julia>

> <https://stackoverflow.com/questions/26071317/declaring-top-level-variables-in-julia-using-metaprogramming/26071597#26071597>

> <https://stackoverflow.com/questions/35051773/create-local-variables-programmatically-from-a-dictionary-in-julia>

> [@Listing the names of local variables](https://discourse.julialang.org/t/listing-the-names-of-local-variables/9776/19):
>
> FWIW, this kind of functionality is often easier to implement than it looks. With [MacroTools](https://github.com/MikeInnes/MacroTools.jl#expression-walking): using MacroTools: prewalk using MacroTools # Your example: expr = quote function readfile(filename::String) a = Dict{String,Int}() buf = "" open(filename,"r") do ios while(!eof(ios)) bud = strip(readline(ios)) items = split(buf,"") a[items[1]] = length(items) end end return a end end # Gather all local variables vars = [] prewalk(expr) do x …

Thank you,

Gordon

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [March 15, 2020, 7:08am UTC](https://discourse.julialang.org/t/initializing-a-dataframe/35933/22 "2020-03-15T07:08:24Z")

</div>

> [@erlebach](#):
>
> Creating easy to use functionality that is not simple with the current structure of julia and packaging it for the user.
> 
> I am open to any suggestions you might have. This little excursion has taught me about NamedTuples, Structures, zip, loops of various kinds and the most basic form of metaprogramming.

I suspect that there is a simple and idiomatic solution, but I have to admit that with this meandering topic I no longer have a clear idea what the problem is.

If you want collect the arguments a function was called with, you could define a container and make it callable. Eg

```julia
struct CollectingVector{T}
    vector::Vector{T}
end

CollectingVector{T}() where T = CollectingVector(Vector{T}())

function (cv::CollectingVector)(x)
    push!(cv.vector, x)
    nothing
end

julia> cv = CollectingVector{typeof((J_δ = 1.0, c = 1.0))}(); # template for type

julia> cv((J_δ = 1, c = 2))

julia> cv.vector
1-element Array{NamedTuple{(:J_δ, :c),Tuple{Float64,Float64}},1}:
 (J_δ = 1.0, c = 2.0)

```

You [almost certainly don’t need metaprogramming](https://discourse.julialang.org/t/how-to-warn-new-users-away-from-metaprogramming/35022), but instead of learning about very basic things like loops and composite types (`stuct`) in the course of solving the problem, you may benefit from just working through the manual first.

Julia is a powerful language, but you won’t be able to harness that power without making an initial investment in some structured form.

---

<div class="post-metadata">

**Author:** ![roble](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roble/32/12303_2.png) [@roble](https://discourse.julialang.org/u/roble)\
**Post date:** [March 15, 2020, 8:44am UTC](https://discourse.julialang.org/t/initializing-a-dataframe/35933/23 "2020-03-15T08:44:27Z")

</div>

> [@erlebach](#):
>
> Is there a shortcut notation so that I do not have to type Vector{Int} 15 times? More generally, supposed I do not know ahead

There is:

```julia
colnames= Symbol.('A':'Z')
df = DataFrame(fill(Int, length(colnames)), colnames)

#now you can push your data line by line onto your df
for line in eachrow(rand(1:100, 100, length(colnames)))
    push!(df, Tuple(line))
end

```

The trick does the Array(created by fill) which contains the type of each column.

---

<div class="post-metadata">

**Author:** ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)\
**Post date:** [March 15, 2020, 12:34pm UTC](https://discourse.julialang.org/t/initializing-a-dataframe/35933/24 "2020-03-15T12:34:09Z")

</div>

Thank you, everybody, I have learned a lot from you, beyond my initial studies of the language. I appreciate it.

Just for reference, I have done lots of reading, and experimentations, but have formed first impressions of the language. Another possible approach I also had not considered is using the possibly right tool for the job, if efficiency is not an: calling Python code. I will certainly keep reading all the great information out there and close the issue, which has certainly meandered.

[Previous page](https://discourse.julialang.org/t/initializing-a-dataframe/35933.md?page=1)
