【问题标题】:reading CSV file into julia DataFrame将 CSV 文件读入 julia DataFrame
【发布时间】:2021-05-05 01:33:37
【问题描述】:

当我尝试将 csv 文件导入到 this blogpost 中描述的 Julia DataFrame 时,我得到了意想不到的结果。

DataFrame() 将每个 CSV.Row 放入一个单元格而不是每个字段。 CSV.jl 似乎不是问题,因为 CSV.Row 对象是正确的。有人看到我在这里缺少什么吗?

example.csv

col1,col2,col3
1,2,3
2,3,5
0,1,1
using CSV
using DataFrames

DataFrame(CSV.File("example.csv"))

结果

x1 x2 x3
CSV.Row: (col1 = 1, col2 = 2, col3 = 3) CSV.Row: (col1 = 2, col2 = 3, col3 = 5) CSV.Row: (col1 = 0, col2 = 1, col3 = 1)

预期结果

col1 col2 col3
1 2 3
2 3 5
0 1 1

编辑

我正在使用:

CSV v0.8.3

DataFrames v0.13.1

【问题讨论】:

    标签: dataframe csv julia


    【解决方案1】:

    确保您使用的是最新的 DataFrames.jl 0.22.5 和 CSV.jl 0.8.3。在他们之下,我得到了你所要求的:

    julia> str = """col1,col2,col3
           1,2,3
           2,3,5
           0,1,1""";
    
    julia> DataFrame(CSV.File(IOBuffer(str)))
    3×3 DataFrame
     Row │ col1   col2   col3  
         │ Int64  Int64  Int64 
    ─────┼─────────────────────
       1 │     1      2      3
       2 │     2      3      5
       3 │     0      1      1
    

    【讨论】:

    • 谢谢,快速回复。我的 DataFrames 版本确实是问题所在。
    猜你喜欢
    • 1970-01-01
    • 2017-02-16
    • 2018-09-10
    • 2013-05-31
    • 2021-06-22
    • 2012-12-31
    相关资源
    最近更新 更多