Ajit Jain
Ajit Jain

Reputation: 1

Julia Concatenating dataframes in loop

I have files in folder that I want to read all at once, bind and make new dataframe.

Code:

using CSV, DataFrames

path="somepath"

files=readdir(path)

d=DataFrame()

for file=files

    x=CSV.read(file)

    d=vcat(d,x)

end

Produces:

Error: UndefVarError: d not defined

Upvotes: 0

Views: 454

Answers (1)

Bogumił Kamiński
Bogumił Kamiński

Reputation: 69949

You can use append! in such cases (change allowing this approach is on master but is not released yet):

d=DataFrame()
for file=files
    append!(d, CSV.read(file))
end

or, if you want to use vcat (this option will use a bit more memory):

reduce(vcat, [CSV.read(file) for file in files])

The original code should be rewritten as:

d=DataFrame()
for file=files
    x=CSV.read(file)
    global d=vcat(d,x)
end

(note global in front of d) but this is not a recommended way to perform this operation.

Upvotes: 1

Related Questions