Georgery
Georgery

Reputation: 8117

Vector from Dataframe Column in Julia

I have a DataFrame

df = DataFrame(x = 1:3, y = 4:6)

3×2 DataFrame
 Row │ x      y     
     │ Int64  Int64 
─────┼──────────────
   1 │     1      4
   2 │     2      5
   3 │     3      6

How can I extract one of the columns as a Vector?

I know I can do df[:,:x] or df.x, but is there a way to do it with a function instead? The reason I am asking is that I work with the Chain.jl package and would like to do something like

@chain df begin
    some_manipulations_here
    pull(:x)
end

Upvotes: 4

Views: 2223

Answers (2)

Bogumił Kamiński
Bogumił Kamiński

Reputation: 69949

You can do one of the the following:

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

julia> @chain df begin
       _.x
       end
3-element Vector{Int64}:
 1
 2
 3

julia> @chain df begin
       getproperty(:x) # same as above
       end
3-element Vector{Int64}:
 1
 2
 3

julia> @chain df begin
       getindex(!, :x) # also _[!, :x]
       end
3-element Vector{Int64}:
 1
 2
 3

julia> @chain df begin
       getindex(:, :x) # also _[:, :x]
       end
3-element Vector{Int64}:
 1
 2
 3

Probably the first option (with _.x is the simplest in practice.

I have shown the other options to highlight that all special syntaxes like df.x or df[!, :x] are actually function calls, and the special syntax is just a convenience.

Upvotes: 4

Georgery
Georgery

Reputation: 8117

OK, so one solution is

@chain df begin
    some_manipulations_here
    _ |> df -> df.x
end

but I actually hope that someone can come up with a cleaner solution.

Upvotes: 2

Related Questions