将现有且已填充的Julia数据帧写入CSV输出时出错

问题描述 投票:2回答:1

我有一个数据框,该数据框是在不同的行上执行各种计算的。我正在尝试将数据帧写入csv输出,但始终收到以下错误:ArgumentError: `nothing` should not be printed; use `show`, `repr`, or custom output instead.

这里是代码:

filename = "Alldata.csv"
CSV.write(filename, bf)

这里是堆栈跟踪:

 [2] print_to_string(::Nothing) at .\strings\io.jl:123
 [3] string(::Nothing) at .\strings\io.jl:156
 [4] writecell(::Array{UInt8,1}, ::Int32, ::Int32, ::IOStream, ::Nothing, ::CSV.Options{UInt8,UInt8,Nothing,Tuple{}}) at C:\Users\haley.sims\.julia\packages\CSV\ztQqu\src\write.jl:281
 [5] macro expansion at C:\Users\haley.sims\.julia\packages\CSV\ztQqu\src\write.jl:182 [inlined]
 [6] eachcolumn at C:\Users\haley.sims\.julia\packages\Tables\FXXeK\src\utils.jl:49 [inlined]
 [7] writerow(::Array{UInt8,1}, ::Base.RefValue{Int32}, ::Int32, ::IOStream, ::Tables.Schema{(:bid, :building_name, :constr_year, :floor_area, :primary_type, :primary_
 [8] (::getfield(CSV, Symbol("##60#61")){getfield(CSV, Symbol("##53#54")){Bool,Tables.Schema{(:bid, :building_name, :constr_year, :floor_area, :primary_type, :primary_occupancy, :n_above, :period, :peak_pop, :eco_pop, :demo_cost, :renew_cost, :replacement_cost......
[9] #open#310(::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}, ::Function, ::getfield(CSV, Symbol("##60#61")){getfie.....
 [10] open at .\iostream.jl:367 [inlined]
 [11] with(::getfield(CSV, Symbol("##53#54")){Bool,Tables.Schema{(:bid, :building_name, :constr_year, :floor_area, :primary_type, :primary_occupancy, :n_above, :period, :peak_pop, :eco_pop, :demo_cost, :renew_cost, :replacement_cost, :retro_cost, :retro_cost_notes, :perc_1, :perc_2, :perc_3, :perc_4, :perc_5, :perc_6, :perc_7, :perc_8, :perc_9, :perc_10, :perc_11, :n_col, :IM_43, :IM_200, :IM_475, :IM_2475, :PC_43, :PC_200, :PC_475, :PC_2475, :renew_cost3, :temp_cost, :tier3, :fr_HAZ, :cole_HAZ, :IM_975, :collapse_ext, :pcim_975, :theta3, :beta3, :theta4, :beta4, :tier4, :aaf4, :aafr, :pf4_50, :pfr_50, :annual_collapse, :pocc, :pf_col, :ri....
 [12] #write#52(::Bool, ::Bool, ::Array{String,1}, ::Function, ::Tables.Schema{(:bid, :building_name, :constr_year, :floor_area, :primary_type, :primary_occupancy, :n_above, :period, :peak_pop, :eco_pop, :demo_cost, :renew_cost, :replacement_cost, :retro_cost, :retro_cost_notes....
[13] write(::Tables.Schema{(:bid, :building_name, :constr_year, :floor_area, :primary_type, :primary_occupancy, :n_above, :period, :peak_pop, :eco_pop, :demo_cost, :renew_cost, :replacement_cost, :retro_cost, :retro_cost_notes, :perc_1, :perc_2, :perc_3, :perc_4, :perc_5, :perc....
[14] #write#51(::Char, ::Char, ::Nothing, ::Nothing, ::Char, ::Char, ::Char, ::Nothing, ::Bool, ::String, ::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}, ::typeof(CSV.write), ::String, ::DataFrame) at C:\Users\haley.sims\.julia\packages\CSV\ztQqu\src\write.jl:60
 [15] write(::String, ::DataFrame) at C:\Users\haley.sims\.julia\packages\CSV\ztQqu\src\write.jl:53
 [16] top-level scope at In[177]:3

该数据框存在并已填充,因为我可以打印它:

│ Row │ bid    │ building_name                       │ constr_year │ floor_area │ primary_type │ primary_occupancy │ n_above │ period   │ peak_pop │ eco_pop │ demo_cost │ renew_cost │ replacement_cost │ retro_cost │ retro_cost_notes                                                                                                                                                                                                                                       │ perc_1   │ perc_2   │ perc_3   │ perc_4   │ perc_5   │ perc_6   │ perc_7   │ perc_8   │ perc_9   │ perc_10  │ perc_11  │ n_col  │ IM_43 │ IM_200 │ IM_475 │ IM_2475 │ PC_43 │ PC_200  │ PC_475  │ PC_2475 │ renew_cost3 │ temp_cost │ tier3  │ fr_HAZ   │ cole_HAZ │ IM_975 │ collapse_ext │ pcim_975 │ theta3  │ beta3    │ theta4  │ beta4    │ tier4  │ aaf4      │ aafr       │ pf4_50   │ pfr_50   │ annual_collapse │ pocc     │ pf_col   │ risk_individual │ risk_individual_perc │
│     │ String │ String                              │ Int64⍰      │ Int64      │ String       │ String⍰           │ Int64⍰  │ Float64⍰ │ Float64  │ Float64 │ Missing   │ Float64⍰   │ Float64          │ Float64⍰   │ Union{Missing, String}                                                                                                                                                                                                                                 │ Float64⍰ │ Float64⍰ │ Float64⍰ │ Float64⍰ │ Float64⍰ │ Float64⍰ │ Float64⍰ │ Float64⍰ │ Float64⍰ │ Float64⍰ │ Float64⍰ │ Int64⍰ │ Int64 │ Int64  │ Int64  │ Int64   │ Int64 │ Float64 │ Float64 │ Float64 │ Float64⍰    │ Float64⍰  │ String │ Float64⍰ │ Float64⍰ │ Int64  │ Float64⍰     │ Float64⍰ │ Float64 │ Float64  │ Float64 │ Float64  │ Union… │ Float64   │ Float64    │ Float64  │ Float64  │ Float64         │ Float64  │ Float64  │ Float64         │ Float64              │
├─────┼────────┼─────────────────────────────────────┼─────────────┼────────────┼──────────────┼───────────────────┼─────────┼──────────┼──────────┼─────────┼───────────┼────────────┼──────────────────┼────────────┼────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼──────────┼──────────┼──────────┼──────────┼──────────┼──────────┼──────────┼──────────┼──────────┼──────────┼──────────┼────────┼───────┼────────┼────────┼─────────┼───────┼─────────┼─────────┼─────────┼─────────────┼───────────┼────────┼──────────┼──────────┼────────┼──────────────┼──────────┼─────────┼──────────┼─────────┼──────────┼────────┼───────────┼────────────┼──────────┼──────────┼─────────────────┼──────────┼──────────┼─────────────────┼──────────────────────┤
│ 1   │ 22     │ LOWER MALL RESEARCH STATION         │ 1960        │ 75229      │ C2           │ Laboratory        │ 4       │ 0.37     │ 206.874  │ 68.9579 │ missing   │ 47.8929    │ 56.3446          │ missing    │ Lab wing 95% vulnerable - Office wing far less vulnerable at 5% collapse. New shear and frame ductility required.  Non structural risk high.                                                                                                           │ 0.85     │ 0.85     │ 0.9      │ 0.9      │ 0.8      │ 0.85     │ 0.9      │ 0.9      │ 0.9      │ 0.85     │ 0.9      │ 11     │ 43    │ 200    │ 475    │ 2475    │ 0     │ 0.0     │ 0.098   │ 0.42    │ 22.969      │ 47.8929   │ III    │ missing  │ missing  │ 975    │ 0.872727     │ 0.9      │ 2294.63 │ 0.951074 │ 572.462 │ 0.398816 │ V      │ 0.0970286 │ 0.00312973 │ 0.992183 │ 0.144857 │ 0.00179141      │ 0.333333 │ 0.785455 │ 0.000469024     │ 0.0469024            │
│ 2   │ 48     │ ANTHROPOLOGY AND SOCIOLOGY BUILDING │ 1975        │ 72384      │ C2           │ Office            │ 3       │ 0.2      │ 267.931  │ 60.2845 │ missing   │ 24.1623    │ 25.4339          │ 21.6189    │ Wings B and C highly vulnerable to collapse.  Lateral strength weakness very high, connections of roofs minimal. Mitigation would need to address adjacent ANSO buildings. Modelling includes assumtions about soil stability and foundation response. │ 0.95     │ 0.5      │ 0.5      │ 0.5      │ 0.5      │ 0.5      │ 0.7      │ 0.7      │ 0.5      │ 0.5      │ 0.5      │ 11     │ 43    │ 200    │ 475    │ 2475    │ 0     │ 0.0     │ 0.016   │ 0.582   │ 16.414      │ 24.1623   │ IV     │ missing  │ missing  │ 975    │ 0.577273     │ 0.9      │ 2111.45 │ 0.657641 │ 572.462 │ 0.398816 │ V      │ 0.0561079 │ 0.00273608 │ 0.939517 │ 0.127859 │ 0.00179141      │ 0.225    │ 0.519545 │ 0.000209412     │ 0.0209412            │
│ 3   │ 148    │ CHEMISTRY B BLOCK, SOUTH WING       │ 1959        │ 73590      │ C1           │ Laboratory        │ 3       │ 0.51     │ 180.63   │ 60.21   │ missing   │ 41.1035    │ 48.357           │ missing    │ missing                                                                                                                                                                                                                                                │ 0.05     │ 0.05     │ 0.2      │ 0.1      │ 0.1      │ 0.0      │ 0.3      │ 0.3      │ 0.3      │ 0.8      │ 0.3      │ 10     │ 43    │ 200    │ 475    │ 2475    │ 0     │ 0.0     │ 0.0     │ 0.517   │ 26.943      │ 48.357    │ IV     │ missing  │ missing  │ 975    │ 0.25         │ 0.76     │ 2452.13 │ 0.217791 │ 753.719 │ 0.425775 │ V      │ 0.018312  │ 0.0027327  │ 0.599724 │ 0.127711 │ 0.00135172      │ 0.333333 │ 0.225    │ 0.000101379     │ 0.0101379 ```       


csv dataframe julia
1个回答
0
投票

您可能正在使用Julia 1.0。尝试切换到Julia 1.3.1(当前版本),问题应该消失。

[如果您必须坚持使用Julia 1.0,则问题在于此版本中不允许打印nothing值(Julia 1.3.1现在允许使用,因此这就是切换解决您的问题的原因)。为了解决该问题,最简单的方法是将nothing替换为missing(因为如果您的数据中包含nothing,则其中不使用missing,因此使用它作为替代品应该是安全的) )。

在这种情况下有效的代码是:

filename = "Alldata.csv"
CSV.write(filename, something.(bf, missing))

在保存的文件中执行此操作后,原始数据帧中每次出现的nothing都将存储为特定行中的空白字段(并且当您使用CSV.read读回文件时,将需要一个missing结果数据框中的值)。

© www.soinside.com 2019 - 2024. All rights reserved.