Easier access to DataFrame's elements

julia> using DataFrames

julia> df = DataFrame(A = 1:4, B = ["M", "F", "F", "M"])
4×2 DataFrame
│ Row │ A     │ B      │
│     │ Int64 │ String │
├─────┼───────┼────────┤
│ 1   │ 1     │ M      │
│ 2   │ 2     │ F      │
│ 3   │ 3     │ F      │
│ 4   │ 4     │ M      │

julia> df1 = df[1, :]
1×2 DataFrame
│ Row │ A     │ B      │
│     │ Int64 │ String │
├─────┼───────┼────────┤
│ 1   │ 1     │ M      │

julia> df1.A
1-element Array{Int64,1}:
 1

julia> df1[:A]
1-element Array{Int64,1}:
 1

Is there a way to define a df1 (for a row) so that both/either df1.A and/or df1[:A] get me the Int64 element rather than a 1-element Array{Int64,1}? I hope to do away with the syntax of df1.A[1] or df1[:A][1].

EDIT: best way I found is:

df1 = DataFrameRow(df, 1)

One option:

df1 = getfield(df[1,:], :colindex).lookup # returns the dict that holds the data

df1[:A] # 1
1 Like

DataFramesMeta.jl has a number of great convenience features for DataFrames. Depending on what you want to do with the row information, @byrow might fit your use case:

let x = 0.0
    @byrow! df begin
        if :A < :B
            x += :B * :C
        end
    end
    x
end
2 Likes
julia> df = DataFrame(A = 1:4, B = ["M", "F", "F", "M"])
4×2 DataFrame
│ Row │ A     │ B      │
│     │ Int64 │ String │
├─────┼───────┼────────┤
│ 1   │ 1     │ M      │
│ 2   │ 2     │ F      │
│ 3   │ 3     │ F      │
│ 4   │ 4     │ M      │

julia> df1 = getfield(df[1,:], :colindex).lookup
Dict{Symbol,Int64} with 2 entries:
  :A => 1
  :B => 2

julia> df1[:A]
1

julia> df1[:B]
2

The last is not right. I want “M” returned.

Apologies, even the first one was incorrect! I simply got a Dict with the column index, so the 1 returned wasn’t the 1 in df[1, :A] but simply the index of column :A which happens to be 1 as well! Discard my answer!

It appears like you’re looking for this:

1 Like

That sounds promising. Look forward to it