Ways to order a `DataFrame` by mixed order?

#1

I wanted to order a DataFrame by certain mixed order, e.g. i want to order a DataFrame by a_str in
descending order followed by b_str in ascending order

Setting up data

using DataFrames, Query

a_str = string.(rand(UInt16, 100))
b_str = string.(rand(UInt16,100))

df = DataFrame(a_str = a_str, b_str = b_str)

DataFrames.jl

@> begin
  df
  sort!([order(:a_str, rev = true), :b_str])
end

**DataFramesMeta.jl **

Not possible. See: the DataFramesMeta.jl had an outstanding issue from 2017

Query.jl way

#const Q = Query
@> begin
  df
  @orderby_descending _.a_str
  @thenby _.b_str
end

Query.jl has an implementation. But I think Query.jl used to have some significant performance issues.

0 Likes

#2

A more natural way to write the Query.jl version would be:

df |>
  @orderby_descending(_.a_str) |>
  @thenby(_.b_str) |> 
  DataFrame

You can just use the normal julia pipe operator.

0 Likes

#3

I like not having to type |>. In Rstudio %>% is mapped to Ctrl-Shift-m which is more convenient.

0 Likes