Julia 设置数据帧对象的值

Julia 设置数据帧对象的值,julia,Julia,在本例中,我使用csv模块加载以下csv数据(),并按如下方式导入: using DataFrames using CSV raw = CSV.read("data.csv") raw[1, :location] = "AA" 然后我想通过如下方式对字符串列进行索引来设置它: using DataFrames using CSV raw = CSV.read("data.csv") raw[1, :location] = "AA" 我得到以下错误: setindex! not def

在本例中,我使用csv模块加载以下csv数据(),并按如下方式导入:

using DataFrames
using CSV

raw = CSV.read("data.csv")
raw[1, :location] = "AA"
然后我想通过如下方式对字符串列进行索引来设置它:

using DataFrames
using CSV

raw = CSV.read("data.csv")
raw[1, :location] = "AA"
我得到以下错误:

setindex! not defined for CSV.Column{String,PooledString}

Stacktrace:
 [1] error(::String, ::Type) at ./error.jl:42
 [2] error_if_canonical_setindex(::IndexLinear, ::CSV.Column{String,PooledString}, ::Int64) at ./abstractarray.jl:1006
 [3] setindex!(::CSV.Column{String,PooledString}, ::String, ::Int64) at ./abstractarray.jl:997
 [4] insert_single_entry!(::DataFrame, ::String, ::Int64, ::Symbol) at /home/chris/.julia/packages/DataFrames/S3ZFo/src/dataframe/dataframe.jl:452
 [5] setindex!(::DataFrame, ::String, ::Int64, ::Symbol) at /home/chris/.julia/packages/DataFrames/S3ZFo/src/dataframe/dataframe.jl:491
 [6] top-level scope at In[31]:1
这与我的类型有关,还是我完全做错了什么?举个简单的例子,它似乎是这样工作的:

df=DataFrames.DataFrame(A=[1,2],B=[3,4])

df[2,:A]=7
这是因为默认情况下返回的是一个不可变的数据帧,其底层存储基于
CSV.Column
类型。您可以使用
copycols
选项直接读取可变数据帧:

julia> using CSV

       # Reading from a buffer for the sake of the example
julia> buf = IOBuffer("A B\n1 2\n3 4\n");

       # Note the copycols=true keyword argument
julia> data = CSV.read(buf, copycols=true)
2×2 DataFrames.DataFrame
│ Row │ A     │ B     │
│     │ Int64 │ Int64 │
├─────┼───────┼───────┤
│ 1   │ 1     │ 2     │
│ 2   │ 3     │ 4     │

       # The dataframe is now mutable
julia> data[2, :A] = 42;
julia> data
2×2 DataFrames.DataFrame
│ Row │ A     │ B     │
│     │ Int64 │ Int64 │
├─────┼───────┼───────┤
│ 1   │ 1     │ 2     │
│ 2   │ 42    │ 4     │
另一种可能是从CSV文件加载数据后,将数据转换为“常规”(即基于
数组的、可变的)
数据帧

例如:

julia> using DataFrames

       # Reading from a buffer for the sake of the example
julia> buf = IOBuffer("A B\n1 2\n3 4\n");

       # Pass the output of CSV.read to the DataFrame constructor
julia> data = CSV.read(buf) |> DataFrame
2×2 DataFrame
│ Row │ A     │ B     │
│     │ Int64 │ Int64 │
├─────┼───────┼───────┤
│ 1   │ 1     │ 2     │
│ 2   │ 3     │ 4     │

       # The dataframe is now mutable
julia> data[2, :A] = 42;
julia> data
2×2 DataFrame
│ Row │ A     │ B     │
│     │ Int64 │ Int64 │
├─────┼───────┼───────┤
│ 1   │ 1     │ 2     │
│ 2   │ 42    │ 4     │

谢谢你的快速回答!您是对的,使用CSV模块,数据帧将作为只读()读入。在提示后阅读文档会发现另一种可能性:
df=CSV.read(“data.CSV”,copycols=true)
会导致可变数据帧。