ilyagr
1
The following doesn’t work because latter definitions can’t depend on previous ones:
DataFrame(bad=[1,2], good=[0, 3]) |> @map((
count = _.bad + _.good,
bad_rate = _.bad/_.count,
bad_pct =round(_.bad_rate*100;digits=1)
))
Is there any convenient way to do such things? I thought @mutate
might work, but it doesn’t.
ilyagr
2
I think I’ve found a way to do this using the LINQ @let
syntax. It’s a little longer, but it seems pretty clear to me.
df = DataFrame(bad=[1,2], good=[0, 3])
@from i in df begin
@let count = i.bad + i.good
@let bad_rate = i.bad/count
@let bad_pct =round(bad_rate*100;digits=1)
@select {i.good, i.bad, count, bad_rate, bad_pct}
@collect DataFrame # Optional
end
The two styles can even be combined if necessary, with only a little contortion:
DataFrame(bad=[1,2], good=[0, 3]) |> df -> (
@from i in df begin
@let count = i.bad + i.good
@let bad_rate = i.bad/count
@let bad_pct =round(bad_rate*100;digits=1)
@where bad_rate > 0.7
@select {i.good, i.bad, count, bad_rate, bad_pct}
end) |> save("somewhere.csv")
I would write this as
DataFrame(bad=[1,2], good=[0, 3]) |>
@mutate(count = _.bad + _.good) |>
@mutate(bad_rate = _.bad/_.count) |>
@mutate(bad_pct =round(_.bad_rate*100;digits=1))