Julia DataFrame replace or avoid generation of 'missing'

时间:2019-04-16 22:25:14

标签: dataframe julia

I have a DataFrame that contains losses by year and I am aggregating it in the following way

df = DataFrames.DataFrame(Year = Int64[], Loss = Float64[])

push!(df, (1, 15))
push!(df, (3, 30))
push!(df, (3, 23))
push!(df, (4, 45))

aal = by(df, :Year, AAL=:Loss=>sum)
tot_aal = join(DataFrame(Year = 1:5), aal, on = :Year, kind = :left)

This is what I get:

Year      AAL
1        15.0
2     missing
3        53.0
4        45.0
5     missing

Which is ok, however I can't find a clear way to prevent the missing to appear, I would be happy to have just zeroes on the missing joins...

Any suggestion on how to make this better is also welcome :)

Thanks a lot

1 个答案:

答案 0 :(得分:1)

您也可以通过编写以下内容来实现自己的目标:

owner_id

或就位:

<head>
    <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
    </head>
<html>
<body>
    <button onclick="TellTale()">Tell me a story</button>
    <p id="Tale"></p>  <!-- Result from TellTale() will be put here as innerHTML-->  
</body>
<script> 
function TellTale() {
 swal("How do people call you, oh Noble Knight?", {
   content: "input"
 }).then(KnightName => document.getElementById("Tale").innerHTML = "Once upon a time, there lived a champion, noble Sir " + KnightName + ".")
}
</script>
</html>

要在创建julia> sort!([aal; DataFrame(Year=setdiff(1:5, aal.Year), AAL=0.0)]) 5×2 DataFrame │ Row │ Year │ AAL │ │ │ Int64 │ Float64 │ ├─────┼───────┼─────────┤ │ 1 │ 1 │ 15.0 │ │ 2 │ 2 │ 0.0 │ │ 3 │ 3 │ 53.0 │ │ 4 │ 4 │ 45.0 │ │ 5 │ 5 │ 0.0 │ 之后将其固定到位,可以编写:

julia> sort!(append!(aal, DataFrame(Year=setdiff(1:5, aal.Year), AAL=0.0)))
5×2 DataFrame
│ Row │ Year  │ AAL     │
│     │ Int64 │ Float64 │
├─────┼───────┼─────────┤
│ 1   │ 1     │ 15.0    │
│ 2   │ 2     │ 0.0     │
│ 3   │ 3     │ 53.0    │
│ 4   │ 4     │ 45.0    │
│ 5   │ 5     │ 0.0     │

这有点冒险,因为通常tot_aal最初可能包含julia> replace!(tot_aal.AAL, missing=>0.0) 5-element Array{Union{Missing, Float64},1}: 15.0 0.0 53.0 45.0 0.0 julia> tot_aal 5×2 DataFrame │ Row │ Year │ AAL │ │ │ Int64 │ Float64⍰ │ ├─────┼───────┼──────────┤ │ 1 │ 1 │ 15.0 │ │ 2 │ 2 │ 0.0 │ │ 3 │ 3 │ 53.0 │ │ 4 │ 4 │ 45.0 │ │ 5 │ 5 │ 0.0 │ 个值(然后您将替换所有aal而不管它们的来源)。因此,您可以考虑编写如下内容:

missing

或将最后一步替换为:

missing