Custom sort for SubDataFrame

Viewed 206

I'm trying to apply a custom sorting algorithm to a bunch of subdataframes in order to make some plots. With the help of this question, I'm able to sort my dataframe with a custom order:

julia> using DataFrames

julia> df = DataFrame(x = rand(10), y = rand([:low, :med, :high], 10), z = rand([:a, :b], 10))
10×3 DataFrames.DataFrame
│ Row │ x         │ y    │ z │
├─────┼───────────┼──────┼───┤
│ 1   │ 0.436891  │ low  │ b │
│ 2   │ 0.370725  │ high │ b │
│ 3   │ 0.521269  │ low  │ b │
│ 4   │ 0.071102  │ high │ a │
│ 5   │ 0.969407  │ high │ a │
│ 6   │ 0.0416023 │ med  │ b │
│ 7   │ 0.63486   │ med  │ b │
│ 8   │ 0.4352    │ high │ b │
│ 9   │ 0.626739  │ low  │ b │
│ 10  │ 0.151149  │ low  │ a │

julia> o = [:low, :med, :high]
3-element Array{Symbol,1}:
 :low 
 :med 
 :high

julia> custom_sort(x,y) = findfirst(o, x) < findfirst(o, y)
custom_sort (generic function with 1 method)

julia> sort!(df, cols=[:y], lt=custom_sort)
10×3 DataFrames.DataFrame
│ Row │ x         │ y    │ z │
├─────┼───────────┼──────┼───┤
│ 1   │ 0.436891  │ low  │ b │
│ 2   │ 0.521269  │ low  │ b │
│ 3   │ 0.626739  │ low  │ b │
│ 4   │ 0.151149  │ low  │ a │
│ 5   │ 0.0416023 │ med  │ b │
│ 6   │ 0.63486   │ med  │ b │
│ 7   │ 0.370725  │ high │ b │
│ 8   │ 0.071102  │ high │ a │
│ 9   │ 0.969407  │ high │ a │
│ 10  │ 0.4352    │ high │ b │

and it works great. The trouble is, when I then do a groupby(), the custom sorting gets lost:

julia> groupby(df, [:y, :z])
DataFrames.GroupedDataFrame  5 groups with keys: Symbol[:y, :z]
First Group:
2×3 DataFrames.SubDataFrame{Array{Int64,1}}
│ Row │ x        │ y    │ z │
├─────┼──────────┼──────┼───┤
│ 1   │ 0.071102 │ high │ a │
│ 2   │ 0.969407 │ high │ a │
⋮
Last Group:
2×3 DataFrames.SubDataFrame{Array{Int64,1}}
│ Row │ x         │ y   │ z │
├─────┼───────────┼─────┼───┤
│ 1   │ 0.0416023 │ med │ b │
│ 2   │ 0.63486   │ med │ b │

Is there a way I can sort the SubDataFrames so that eg. the first group is has y == :low and z == a?

1 Answers
Related