我在文件夹中有文件,我想一次读取所有文件,绑定并创建新的数据帧。
法典:
using CSV, DataFrames
path="somepath"
files=readdir(path)
d=DataFrame()
for file=files
x=CSV.read(file)
d=vcat(d,x)
end
生产:
Error: UndefVarError: d not defined
在这种情况下
,您可以使用append!
(允许此方法的更改在master上,但尚未发布(:
d=DataFrame()
for file=files
append!(d, CSV.read(file))
end
或者,如果要使用vcat
(此选项将使用更多内存(:
reduce(vcat, [CSV.read(file) for file in files])
原始代码应重写为:
d=DataFrame()
for file=files
x=CSV.read(file)
global d=vcat(d,x)
end
(注意global
前面d
(,但这不是执行此操作的推荐方法。