william3031
william3031

Reputation: 1708

How do you create a dataframe out of arrays in Julia?

How can I create a dataframe out of separate arrays?

For example, I want this, but 18 rows by two columns.

using DataFrames

df = DataFrame(
year = [[3:1:20;]],
amt = [fill(200, 18)]
)

Upvotes: 1

Views: 603

Answers (1)

Nils Gudat
Nils Gudat

Reputation: 13800

You don't need any arrays:

julia> using DataFrames

julia> df = DataFrame(year = 3:1:20, amt = 200)
18×2 DataFrame      
 Row │ year   amt   
     │ Int64  Int64 
─────┼──────────────
   1 │     3    200 
   2 │     4    200 
   3 │     5    200 
   4 │     6    200 
   5 │     7    200 
   6 │     8    200 
   7 │     9    200 
   8 │    10    200 
   9 │    11    200 
  10 │    12    200 
  11 │    13    200 
  12 │    14    200 
  13 │    15    200 
  14 │    16    200 
  15 │    17    200 
  16 │    18    200 
  17 │    19    200 
  18 │    20    200 

If this seems a bit magical (passing a range object and a single value rather than arrays), you can get the same result if you pass in "real" arrays like DataFrame(year = collect(3:1:20), amt = fill(200, 18)). Note however that this is unnecessary and less efficient.

Also note that your enclosing square brackets are probably not what you're after: fill(200, 18) already creates an array:

julia> fill(200, 18)
18-element Vector{Int64}:
 200
 200

(Vector{Int} is an alias for Array{Int, 1}), while enclosing this in another set of brackets will create an array of length one, which holds your amt array as its only element:


julia> [fill(200, 18)]
1-element Vector{Vector{Int64}}:
 [200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200]

Upvotes: 4

Related Questions