Home > Blockchain >  How to extract column_name String and data Vector from a one-column DataFrame in Julia?
How to extract column_name String and data Vector from a one-column DataFrame in Julia?

Time:09-25

I was able to extract the column of a DataFrame that I want using a regular expression, but now I want to extract from that DataFrame column a String with the column name and a Vector with the data. How can I construct f and g below? Alternate approaches also welcome.

julia> df = DataFrame("x (in)" => 1:3, "y (°C)" => 4:6)
3×2 DataFrame
 Row │ x (in)  y (°C) 
     │ Int64   Int64  
─────┼────────────────
   1 │      1       4
   2 │      2       5
   3 │      3       6

julia> y = df[:, r"y "]
3×1 DataFrame
 Row │ y (°C) 
     │ Int64  
─────┼────────
   1 │      4
   2 │      5
   3 │      6

julia> y_units = f(y)
"°C"

julia> y_data = g(y)
3-element Vector{Int64}:
 4
 5
 6

CodePudding user response:

f(df) = only(names(df))
g(df) = only(eachcol(df)) # or df[!, 1] if you do not need to check that this is the  only column

(only is used to check that the data frame actually has only one column)

An alternate approach to get the column name without creating an intermediate data frame is just writing:

julia> names(df, r"y ")
1-element Vector{String}:
 "y (°C)"

to extract out the column name (you need to get the first element of this vector)

  • Related