How can I get the name property when using eachcol?
1 Like
You are talking about DataFrames.eachcol
right? It seems that in a loop you just get an array.
julia> dfq = DataFrame(a = [3, 4], b = ["ali", "baba"])
2Γ2 DataFrame
β Row β a β b β
β β Int64 β String β
βββββββΌββββββββΌβββββββββ€
β 1 β 3 β ali β
β 2 β 4 β baba β
julia> for col in eachcol(dfq)
println(col, ' ', typeof(col))
end
[3, 4] Array{Int64,1}
["ali", "baba"] Array{String,1}
So what about:
julia> for name in names(dfq)
col = dfq[name]
println(name, ": ", col)
end
a: [3, 4]
b: ["ali", "baba"]
You can also iterate through pairs(eachcol(df))
julia> for (name, col) in pairs(eachcol(df))
@show name
end
5 Likes
I did something similar. I want to know how to access the name property.
Ua! Hard to get it. Donβt know how it works, but yes, this works!
Hereβs how it works:
julia> using DataFrames
julia> df = DataFrame(rand(10, 10));
julia> @which pairs(eachcol(df))
pairs(itr::DataFrames.DataFrameColumns) in DataFrames at /home/nils/.julia/packages/DataFrames/cdZCk/src/abstractdataframe/iteration.jl:200
so the pairs
method being called is implemented here:
Base.pairs(itr::DataFrameColumns) = Base.Iterators.Pairs(itr, keys(itr))
and the DataFrameColumns
object comes from here:
eachcol(df::AbstractDataFrame) = DataFrameColumns(df)
4 Likes