everyone, I am a Julia newbie. I want to write a Function that delete any column with only zeroes.
function delEmptyCol(df)
emptyColList = []
for col in eachcol(df)
if sum(col) == 0
append!(empyColList,col)
end
end
newdf = select!(df,Not(emptyColList))
return newdf
end
and I made up a trial DataFrame df2 to test my Function.It looks like following.
KFC Mc Piz
Int64 Int64 Int64
1 0 1 4
2 0 2 5
3 0 3 6
So what I hope to get is as following.
Mc Piz
Int64 Int64
1 1 4
2 2 5
3 3 6
However,when i do delEmptyCol(df2),I get an error and I have no idea what is wrong.
BoundsError: attempt to access data frame with 3 columns at index [0, 0, 0]
Stacktrace:
[1] getindex
@ C:\Users\cxh\.julia\packages\DataFrames\zqFGs\src\other\index.jl:199 [inlined]
[2] getindex
@ C:\Users\cxh\.julia\packages\DataFrames\zqFGs\src\other\index.jl:257 [inlined]
[3] getindex
@ C:\Users\cxh\.julia\packages\DataFrames\zqFGs\src\other\index.jl:224 [inlined]
[4] manipulate(df::DataFrame, c::InvertedIndex{Vector{Any}}; copycols::Bool, keeprows::Bool, renamecols::Bool)
@ DataFrames C:\Users\cxh\.julia\packages\DataFrames\zqFGs\src\abstractdataframe\selection.jl:1680
[5] #select#492
@ C:\Users\cxh\.julia\packages\DataFrames\zqFGs\src\abstractdataframe\selection.jl:1171 [inlined]
[6] #select!#487
@ C:\Users\cxh\.julia\packages\DataFrames\zqFGs\src\abstractdataframe\selection.jl:873 [inlined]
[7] select!
@ C:\Users\cxh\.julia\packages\DataFrames\zqFGs\src\abstractdataframe\selection.jl:873 [inlined]
[8] delEmptyCol(df::DataFrame)
@ Main .\In[46]:8
[9] top-level scope
@ In[51]:1
[10] eval
@ .\boot.jl:373 [inlined]
[11] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
@ Base .\loading.jl:1196
Please help me !! I'd appreciate!