# Easier access to DataFrame's elements

**URL:** <https://discourse.julialang.org/t/easier-access-to-dataframes-elements/15589>\
**Category:** General Usage\
**Created:** [September 28, 2018, 6:36am UTC](https://discourse.julialang.org/t/easier-access-to-dataframes-elements/15589 "2018-09-28T06:36:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![LeoK987](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leok987/32/4880_2.png) [@LeoK987](https://discourse.julialang.org/u/LeoK987)\
**Post date:** [September 28, 2018, 6:36am UTC](https://discourse.julialang.org/t/easier-access-to-dataframes-elements/15589/1 "2018-09-28T06:36:38Z")

</div>

```julia
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)`

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [September 28, 2018, 9:18am UTC](https://discourse.julialang.org/t/easier-access-to-dataframes-elements/15589/2 "2018-09-28T09:18:41Z")

</div>

One option:

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

df1[:A] # 1

```

---

<div class="post-metadata">

**Author:** ![jtackm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jtackm/32/4784_2.png) [@jtackm](https://discourse.julialang.org/u/jtackm)\
**Post date:** [September 28, 2018, 10:20am UTC](https://discourse.julialang.org/t/easier-access-to-dataframes-elements/15589/3 "2018-09-28T10:20:23Z")

</div>

[DataFramesMeta.jl](https://github.com/JuliaStats/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:

```julia
let x = 0.0
    @byrow! df begin
        if :A < :B
            x += :B * :C
        end
    end
    x
end

```

---

<div class="post-metadata">

**Author:** ![LeoK987](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leok987/32/4880_2.png) [@LeoK987](https://discourse.julialang.org/u/LeoK987)\
**Post date:** [September 28, 2018, 10:44am UTC](https://discourse.julialang.org/t/easier-access-to-dataframes-elements/15589/4 "2018-09-28T10:44:26Z")

</div>

```julia
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.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [September 28, 2018, 11:00am UTC](https://discourse.julialang.org/t/easier-access-to-dataframes-elements/15589/5 "2018-09-28T11:00:19Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [September 28, 2018, 11:11am UTC](https://discourse.julialang.org/t/easier-access-to-dataframes-elements/15589/6 "2018-09-28T11:11:13Z")

</div>

It appears like you’re looking for this:  
[https://github.com/JuliaData/DataFrames.jl/issues/1533](https://github.com/JuliaData/DataFrames.jl/issues/1533)

---

<div class="post-metadata">

**Author:** ![LeoK987](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leok987/32/4880_2.png) [@LeoK987](https://discourse.julialang.org/u/LeoK987)\
**Post date:** [September 28, 2018, 11:30am UTC](https://discourse.julialang.org/t/easier-access-to-dataframes-elements/15589/7 "2018-09-28T11:30:29Z")

</div>

> [@nalimilan](#):
>
> Issue: Behavior of `df[1, cols]` and `DataFrameRow`

That sounds promising. Look forward to it
