Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops 读取循环数据帧中的CSV文件(Julia)_Loops_Dataframe_Julia - Fatal编程技术网

Loops 读取循环数据帧中的CSV文件(Julia)

Loops 读取循环数据帧中的CSV文件(Julia),loops,dataframe,julia,Loops,Dataframe,Julia,我想读取多个名称不断变化的CSV文件,如CSV_1.CSV等。 我的想法是简单地实现如下循环 using CSV for i = 1:8 a[i] = CSV.read("0.$i.csv") end 但很明显,这是行不通的。 有没有一种简单的方法来实现这一点,比如在数据帧中引入一个额外的维度?假设a在这种情况下是一个数组,这是绝对可能的,但要这样做,您需要预先分配数组,因为您不能分配一个还不存在的索引: julia> a = [] 0-element Array{Any,1}

我想读取多个名称不断变化的CSV文件,如CSV_1.CSV等。 我的想法是简单地实现如下循环

using CSV
for i = 1:8
    a[i] = CSV.read("0.$i.csv")
end
但很明显,这是行不通的。 有没有一种简单的方法来实现这一点,比如在数据帧中引入一个额外的维度?

假设a在这种情况下是一个数组,这是绝对可能的,但要这样做,您需要预先分配数组,因为您不能分配一个还不存在的索引:

julia> a = []
0-element Array{Any,1}

julia> a[1] = 1
ERROR: BoundsError: attempt to access 0-element Array{Any,1} at index [1]
Stacktrace:
 [1] setindex!(::Array{Any,1}, ::Any, ::Int64) at ./essentials.jl:455
 [2] top-level scope at REPL[10]:1

julia> a2 = Vector{Int}(undef, 5);

julia> for i in 1:5
           a2[i] = i
       end

julia> a2
5-element Array{Int64,1}:
 1
 2
 3
 4
 5
或者,你可以使用推!根据需要向数组中添加内容

julia> a3 = [];

julia> for i in 1:5
           push!(a3, i)
       end

julia> a3
5-element Array{Any,1}:
 1
 2
 3
 4
 5
所以对于你的CSV文件

using CSV

a = []

for i = 1:8
    push!(a, CSV.read("0.$i.csv"))
end

您可以选择Kevin建议的内容:

# read in the files into a vector
a = CSV.read.(["0.$i.csv" for i in 1:8])

# add an indicator column
for i in 1:8
    a[i][!, :id] .= i
end

# create a single data frame with indicator column holding the source
b = reduce(vcat, a)

您可以使用文件名中的特定模式读取任意数量的CSV文件,为每个文件创建一个数据帧,如果需要,最后创建一个数据帧

using CSV, Glob, DataFrames
path = raw"C:\..." # directory of your files (raw is useful in Windows to add a \)
files=glob("*.csv", path) # to load all CSVs from a folder (* means arbitrary pattern)
dfs = DataFrame.( CSV.File.( files ) ) # creates a list of dataframes

# add an index column to be able to later discern the different sources
for i in 1:length(dfs)
    dfs[i][!, :sample] .= i # I called the new col sample
end

# finally, reduce your collection of dfs via vertical concatenation
df = reduce(vcat, dfs)

为什么它不起作用?你可以定义一个向量{DataFrame},然后用每个CSV的数据帧填充它,或者连接数据帧。问题是a是一个字典,所以这不起作用。你是说最后一步?那么就减少a的值。如果a是dict,有没有办法?是isa AbstractDict还是typeofa@clearseplex对不起,我误解了你的问题。根据需要,您可以通过执行my_dict中的键、值、KeyMy_dict中的k或valuesmy_dict中的v来迭代dict。