如何在 Julia 中使用 CSV 只加载特定列?

S B*_*ois 5 csv julia

我是 Julia 的新手,我一直在寻找一种仅从空格分隔的文件中加载特定列的方法;CSV 的 github 页面上给出的解决方案似乎不起作用(https://github.com/JuliaData/CSV.jl/issues/154),无论是在 Julia 1.0.1 还是 1.3.1 上都不起作用。

这是一个名为 test.txt 的示例输入文件

ab cp
1 2 3
4 5 6

julia> using CSV, Tables  

julia> df = CSV.File(inputfile, delim=" ", header=1, type=String) |> select(:a, :b) |> DataFrame  
ERROR: UndefVarError: select not defined Stacktrace:  [1] top-level scope at none:0

Julia> df = CSV.File("test.txt", delim=" ", header=1, type=String) |> Tables.select(:a, :b) |> DataFrame  
ERROR: UndefVarError: select not defined Stacktrace:  [1] getproperty(::Module, ::Symbol) at ./sysimg.jl:13  [2] top-level scope at none:0
Run Code Online (Sandbox Code Playgroud)

所以,这是我的问题:

  1. 加载 a 列和 b 列的正确语法是什么?
  2. 加载列 a 和 cp 的正确语法是什么?(通过在列名称中使用点分隔)
  3. 从没有标题的文件中按数字加载特定列(例如第 1 列和第 3 列)的正确语法是什么?

谢谢,

斯蒂芬

Bog*_*ski 8

使用此处描述select的选项:CSV.File

julia> CSV.File(inputfile, delim=" ", header=1, type=String, select=[:a,:b])
2-element CSV.File{false}:
 CSV.Row{false}: (a = "1", b = "2")
 CSV.Row{false}: (a = "4", b = "5")

julia> CSV.File(inputfile, delim=" ", header=1, type=String, select=[:a,:var"c.p"])
2-element CSV.File{false}:
 CSV.Row{false}: (a = "1", c.p = "3")
 CSV.Row{false}: (a = "4", c.p = "6")

julia> CSV.File(inputfile, delim=" ", header=1, type=String, select=[1,3])
2-element CSV.File{false}:
 CSV.Row{false}: (a = "1", c.p = "3")
 CSV.Row{false}: (a = "4", c.p = "6")
Run Code Online (Sandbox Code Playgroud)

如果文件没有标头,请使用此处header描述的选项。