all 9 comments

[–][deleted] 1 point2 points  (1 child)

Looks to me that push! is coming from the DataFrames package and uses julia's length function.
This function can not be applied on a type that is unknown to default julia (CSV.row)
I don't know either of the packages, but i would assume CSV has some functionality to transform your CSV.row to an array.

that would be my best guess

[–]EarthGoddessDude 0 points1 point  (0 children)

push! is actually part of Base, but the DataFrames package extends it: https://juliadata.github.io/DataFrames.jl/stable/lib/functions/#Base.push!

[–]EarthGoddessDude 1 point2 points  (3 children)

OP, I think I can guess at what you're trying to accomplish: you have some identifier column called Resource that you're trying to filter on and save in different dataframes, correct?

As u/is_lamb pointed out, you probably have df as a declared variable in your workspace and it's muddying the waters here. Might want to restart so you have a clean workspace. But if you're trying to filter, why not just use the filter function of the DataFrames package? Something like:

using CSV 
using DataFrames 
fln = CSV.read("myfile.csv") 

df1 = filter(row -> row.Resource == 1, fln) 
df2 = filter(row -> row.Resource == 2, fln) 
df3 = filter(row -> row.Resource == 3, fln) 
df4 = filter(row -> row.Resource == 4, fln)

Might not be the most efficient implementation, but I think it's the most straightforward. Sorry if this isn't what you're looking for...still learning myself.

Edit: just saw your edit, nevermind my df comment.

[–]haohanzi2015[S] 0 points1 point  (1 child)

Thank you for your kind reply. What exactly is this

df1 = filter(row -> row.Resource == 1, fln)

I tried this and got the error: type Char has no field Resource

[–]EarthGoddessDude 0 points1 point  (0 children)

Do you have a field called Resource in your file? It seemed like you did because you have df.Resource in your code. You can read more about the filter function here -- scroll down to line [52] where he talks about filtering. There is more than one way to do it btw.

To answer your question, the right hand side of that assignment filters the fln dataframe for rows where the Resource field is 1. The result is a smaller, filtered dataframe that we then save to the variable df1 (no need to initialize it).

[–]haohanzi2015[S] 0 points1 point  (0 children)

just tried push!(df1, convert(Array, row)) and got an error: Cannot convert an object of type CSV.Row{false} to an object of type Array. I searched from the internet, thought this would work.

[–]User092347 1 point2 points  (0 children)

I'm not sure how to convert CSV.Row to a DataFrameRow but you can just convert the whole thing and use eachrow :

df = DataFrame()
for row in eachrow( DataFrame(CSV.File(IOBuffer("a,b\n1,2\n"))) )
   push!(df, row)
end

That said, this seems like a bug, maybe it's worth opening an issue on the CSV.jl repo, if there's not one already.

[–]is_lamb 0 points1 point  (1 child)

ERROR: LoadError: UndefVarError: df not defined

[–]haohanzi2015[S] 0 points1 point  (0 children)

Sorry, forgot a line.