Is there a quick method for specifying a custom order to sort/sort! on Julia DataFrames?
julia> using DataFrames
julia> srand(1);
julia> df = DataFrame(x = rand(10), y = rand([:high, :med, :low], 10))
10×2 DataFrames.DataFrame
│ Row │ x          │ y    │
├─────┼────────────┼──────┤
│ 1   │ 0.236033   │ med  │
│ 2   │ 0.346517   │ high │
│ 3   │ 0.312707   │ high │
│ 4   │ 0.00790928 │ med  │
│ 5   │ 0.488613   │ med  │
│ 6   │ 0.210968   │ med  │
│ 7   │ 0.951916   │ low  │
│ 8   │ 0.999905   │ low  │
│ 9   │ 0.251662   │ high │
│ 10  │ 0.986666   │ med  │
julia> sort!(df, cols=[:y])
10×2 DataFrames.DataFrame
│ Row │ x          │ y    │
├─────┼────────────┼──────┤
│ 1   │ 0.346517   │ high │
│ 2   │ 0.312707   │ high │
│ 3   │ 0.251662   │ high │
│ 4   │ 0.951916   │ low  │
│ 5   │ 0.999905   │ low  │
│ 6   │ 0.236033   │ med  │
│ 7   │ 0.00790928 │ med  │
│ 8   │ 0.488613   │ med  │
│ 9   │ 0.210968   │ med  │
│ 10  │ 0.986666   │ med  │
I would like to have the y column ordered with :low first, followed by :med and :high. What would be the best way of doing this? I know I can do the following:
julia> subdfs = []
0-element Array{Any,1}
julia> for val in [:low, :med, :high]
           push!(subdfs, df[df[:y] .== val, :])
       end
julia> vcat(subdfs...)
10×2 DataFrames.DataFrame
│ Row │ x          │ y    │
├─────┼────────────┼──────┤
│ 1   │ 0.951916   │ low  │
│ 2   │ 0.999905   │ low  │
│ 3   │ 0.236033   │ med  │
│ 4   │ 0.00790928 │ med  │
│ 5   │ 0.488613   │ med  │
│ 6   │ 0.210968   │ med  │
│ 7   │ 0.986666   │ med  │
│ 8   │ 0.346517   │ high │
│ 9   │ 0.312707   │ high │
│ 10  │ 0.251662   │ high │
Is there a way to do this without allocating memory since in my actual example, df is quite large?
You can define a comparison function:
lmhlt(x, y) = x == :low && y != :low || x == :med && y == :high
Then use
sort!(df, lt=lmhlt)
However, this still allocates memory. It should be less than your current version though.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With