DataFrameRow Row Number


#1

If you have a data frame, and want to iterate over each row? How do you access the row number?

data = DataFrame(a = [1, 2, 3], b = ['a', 'b', 'c']) 
r = collect(eachrow(data))[1]

The row number is obviously stored but the normal way of accessing fields does not work

> fieldnames(typeof(r))
(:df, :row)
> r.row
ERROR: KeyError: key :row not found
Stacktrace:
 [1] getindex at ./dict.jl:478 [inlined]
 [2] getindex at /home/paul/.julia/packages/DataFrames/3sRhW/src/other/index.jl:137 [inlined]
 [3] getindex(::DataFrame, ::Int64, ::Symbol) at /home/paul/.julia/packages/DataFrames/3sRhW/src/dataframe/dataframe.jl:265
 [4] getindex at /home/paul/.julia/packages/DataFrames/3sRhW/src/dataframerow/dataframerow.jl:20 [inlined]
 [5] getproperty(::DataFrameRow{DataFrame}, ::Symbol) at /home/paul/.julia/packages/DataFrames/3sRhW/src/dataframerow/dataframerow.jl:37

#2

Looking in the source code, I found two solutions

> data = DataFrame(a = [1, 2, 3], b = ['a', 'b', 'c']) 
> r = collect(eachrow(data))[1]
> DataFrames.row(r)
1
> getfield(r, :row)
1

#3

DataFrames.row is an internal function, so better not use it. I recommend enumerate(eachrow(data)).