Home > Enterprise >  Sort Julia DataFrame in descending Order
Sort Julia DataFrame in descending Order

Time:03-31

I have a DataFrame

using DataFrames
using DataFramesMeta
using Chain

df = DataFrame(a=1:3,b=4:6)

that I want to sort descending by column :a. Doing it ascending is intuitive...

@chain df begin
    @orderby(:a)
end

How can I do this?

Looking for different solutions here, but specifically for a solution that can be used in @chains.

CodePudding user response:

So, if you have a numeric column, you can simply negate it...

using DataFrames
using DataFramesMeta
using Chain

df = DataFrame(a=1:3,b=4:6)

@chain df begin
    @orderby(-:a)
end

But this logic doesn't work for Dates for example.

CodePudding user response:

You can just use sort with rev=true:

julia> @chain df begin
           sort(:a, rev=true)
       end
3×2 DataFrame
 Row │ a      b
     │ Int64  Int64
─────┼──────────────
   1 │     3      6
   2 │     2      5
   3 │     1      4
  • Related