logo

How to Create a DataFrame with Variable Names as Column Names in Julia 📂Julia

How to Create a DataFrame with Variable Names as Column Names in Julia

Overview

Named tuples can be used. The way to create a named tuple is by attaching a semicolon ; right after the left parenthesis. For example, if you say DataFrame(; x, y), a DataFrame is created with column names :x and :y, and the contents are x and y respectively.

Code

julia> MyCol7 = rand(5); B = 1:5;

julia> DataFrame(; MyCol7, B)
5×2 DataFrame
 Row │ MyCol7    B     
     │ Float64   Int64
─────┼─────────────────
   1 │ 0.911763      1
   2 │ 0.93374       2
   3 │ 0.116779      3
   4 │ 0.467364      4
   5 │ 0.473437      5

Environment

  • OS: Windows
  • julia: v1.6.3