Julia中用于转换字符串和滚动均值的类突变运算



我了解如何转换字符串并用逐行的方法计算移动平均值。但我想知道如何通过管道风格的变异方法来实现它们。

julia> first(d1, 10)
10×2 DataFrame
Row │ Year   Exports          
│ Int64  String           
─────┼─────────────────────────
1 │  1960  12.9944524843222
2 │  1961  12.4031007751938
3 │  1962  13.9430116004055
4 │  1963  13.0058857232148
5 │  1964  14.9382483265768
6 │  1965  13.220177939017
7 │  1966  12.9323802481308
8 │  1967  12.8837295006449
9 │  1968  12.2976680384088
10 │  1969  11.9548596244419

d1.Exports = parse.(Float64, d1.Exports)
d1.Exports_4mv = runmean(d1.Exports, 4)

我想要这样的东西:

@linq d1 |>
mutate(:Exports = parse.(Float64, :Exports)) |>
mutate(:Exports_4mv = runmean(:Exports, 4)) 

您可以使用DataFrames.jl(我使用-x函数而不是runmean,因为我不确定您从哪个包中获得了runmean函数(:

julia> using DataFrames, Chain, DataFramesMeta
julia> d1 = DataFrame(Year=1960:1969, Exports=string.(10rand(10) .+ 10))
10×2 DataFrame
Row │ Year   Exports
│ Int64  String
─────┼───────────────────────────
1 │  1960  18.487768691740964
2 │  1961  19.16038389519686
3 │  1962  19.393969741723705
4 │  1963  10.98014563868417
5 │  1964  13.971724415708335
6 │  1965  15.013359289888458
7 │  1966  10.571278054318459
8 │  1967  10.193140593100727
9 │  1968  12.768467572044944
10 │  1969  14.404484209802783
julia> @chain d1 begin
transform(:Exports => ByRow(x -> parse(Float64, x)) => :Exports)
transform(:Exports => (x -> -x) => :Exports_4mv)
end
10×3 DataFrame
Row │ Year   Exports  Exports_4mv
│ Int64  Float64  Float64
─────┼─────────────────────────────
1 │  1960  18.4878     -18.4878
2 │  1961  19.1604     -19.1604
3 │  1962  19.394      -19.394
4 │  1963  10.9801     -10.9801
5 │  1964  13.9717     -13.9717
6 │  1965  15.0134     -15.0134
7 │  1966  10.5713     -10.5713
8 │  1967  10.1931     -10.1931
9 │  1968  12.7685     -12.7685
10 │  1969  14.4045     -14.4045

以及DataFramesMeta.jl 0.7版本:

julia> @chain d1 begin
@transform(Exports = parse.(Float64, :Exports))
@transform(Exports_4mv = -:Exports)
end
10×3 DataFrame
Row │ Year   Exports  Exports_4mv
│ Int64  Float64  Float64
─────┼─────────────────────────────
1 │  1960  18.4878     -18.4878
2 │  1961  19.1604     -19.1604
3 │  1962  19.394      -19.394
4 │  1963  10.9801     -10.9801
5 │  1964  13.9717     -13.9717
6 │  1965  15.0134     -15.0134
7 │  1966  10.5713     -10.5713
8 │  1967  10.1931     -10.1931
9 │  1968  12.7685     -12.7685
10 │  1969  14.4045     -14.4045

最新更新