I am trying to read all files from a folder and trying to create filename variable based on name of the file
I am using the below code to do it. But I am unable to add the variable which lets me know the filename -
using DataFrame
using Queryverse
using VegaLite
using Statistics
using CSV
using Glob
path = "D:\\Udemy\\FInancial_Engineering_Lazy_Programmer\\Yfinance_Data"
files = glob("*.csv", path)
df_com = DataFrame()
for file in files
df = CSV.File(file)
df[:filename] = first(split(last(split(file, "\\")),"."))
append!(df_com, df)
end
I am getting the below error -
ERROR: ArgumentError: invalid index: :filename of type Symbol
Stacktrace:
[1] to_index(i::Symbol)
@ Base .\indices.jl:300
[2] to_index(A::CSV.File{false}, i::Symbol)
@ Base .\indices.jl:277
[3] to_indices
@ .\indices.jl:333 [inlined]
[4] to_indices
@ .\indices.jl:325 [inlined]
[5] setindex!(A::CSV.File{false}, v::Tuple{SubString{String}, Vector{Symbol}}, I::Symbol)
@ Base .\abstractarray.jl:1267
[6] top-level scope
@ .\REPL[161]:3
There is no problem in creating the filename, but there is a problem in adding it to dataframe. The below code works fine and provides the filename, but unable to add it as a variable
for file in files
println(first(split(last(split(file, "\\")),".")))
end
Can you please help?