# Sorting a Table / DataFrame

**URL:** <https://discourse.julialang.org/t/sorting-a-table-dataframe/103475>\
**Category:** General Usage\
**Tags:** dataframes\
**Created:** [September 3, 2023, 2:24am UTC](https://discourse.julialang.org/t/sorting-a-table-dataframe/103475 "2023-09-03T02:24:13Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dan\_Micsa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan_micsa/32/30233_2.png) [@Dan\_Micsa](https://discourse.julialang.org/u/Dan_Micsa)\
**Post date:** [September 3, 2023, 2:24am UTC](https://discourse.julialang.org/t/sorting-a-table-dataframe/103475/1 "2023-09-03T02:24:13Z")

</div>

I have a table like this:

```julia
* A B C
A 1 2 3
B 2 3 4
C 3 4 5

And I want to sort descending both on columns and rows after the sum something like this:

* C B A S
C 5 4 3 12
B 4 3 2 9
A 3 2 1 6
S 12 9 6 *

```

How can I do that the simplest?

thx.

---

<div class="post-metadata">

**Author:** ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)\
**Post date:** [September 3, 2023, 3:44am UTC](https://discourse.julialang.org/t/sorting-a-table-dataframe/103475/2 "2023-09-03T03:44:16Z")

</div>

As far as i know DataFrames does not have row names. It indexes rows from `1:n`.

> [@Rename a row in DataFrames?](https://discourse.julialang.org/t/rename-a-row-in-dataframes/58261/1):
>
> Is it possible to rename a row in DataFrames.jl? Currently it looks like rows are numbered by default 1:n. The function rename!() only changes column names, not row names. My hack is to add a column w/ my desired row names: insertcols!(se, 1, :coef =\> ["β0","β1","β2","β3"]) I can’t find the relevant discussion on whether this was a deliberate design decision…

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [September 3, 2023, 8:12am UTC](https://discourse.julialang.org/t/sorting-a-table-dataframe/103475/3 "2023-09-03T08:12:43Z")

</div>

I assume you do not want the `"*"` and `"S"` columns and do not want the last row in the output. In this case do:

```julia
julia> df = DataFrame("A" => 1:3, "B" => 2:4, "C" => 3:5)
3×3 DataFrame
 Row │ A B C
     │ Int64 Int64 Int64
─────┼─────────────────────
   1 │ 1 2 3
   2 │ 2 3 4
   3 │ 3 4 5

julia> df[sortperm(sum.(eachrow(df)), rev=true), sortperm(sum.(eachcol(df)), rev=true)]
3×3 DataFrame
 Row │ C B A
     │ Int64 Int64 Int64
─────┼─────────────────────
   1 │ 5 4 3
   2 │ 4 3 2
   3 │ 3 2 1

```

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [September 3, 2023, 8:31am UTC](https://discourse.julialang.org/t/sorting-a-table-dataframe/103475/4 "2023-09-03T08:31:21Z")

</div>

```julia
julia> sort(sort(m,dims=1, by=sum, rev=true),dims=2,rev=true)
3×3 Matrix{Int64}:
 5 4 3
 4 3 2
 3 2 1

```

```julia
using LinearAlgebra
rot180(sort(sort(m,dims=1, by=sum),dims=2))

```

---

<div class="post-metadata">

**Author:** ![Dan\_Micsa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan_micsa/32/30233_2.png) [@Dan\_Micsa](https://discourse.julialang.org/u/Dan_Micsa)\
**Post date:** [September 3, 2023, 9:38am UTC](https://discourse.julialang.org/t/sorting-a-table-dataframe/103475/5 "2023-09-03T09:38:09Z")

</div>

Thank you!

Something like this but I need the column label too and the sum if is possible too.

---

<div class="post-metadata">

**Author:** ![Dan\_Micsa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan_micsa/32/30233_2.png) [@Dan\_Micsa](https://discourse.julialang.org/u/Dan_Micsa)\
**Post date:** [September 3, 2023, 9:58am UTC](https://discourse.julialang.org/t/sorting-a-table-dataframe/103475/6 "2023-09-03T09:58:49Z")

</div>

That’s not the main problem I want to know after double sorting a label or index.

---

<div class="post-metadata">

**Author:** ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)\
**Post date:** [September 3, 2023, 10:23am UTC](https://discourse.julialang.org/t/sorting-a-table-dataframe/103475/7 "2023-09-03T10:23:16Z")

</div>

```julia
julia> using DataFrames

julia> M=[1 2 3 ; 2 3 4 ; 3 4 5]
3×3 Matrix{Int64}:
 1 2 3
 2 3 4
 3 4 5

julia> r=sum(M,dims=1)
1×3 Matrix{Int64}:
 6 9 12

julia> l=sum(M,dims=2)
3×1 Matrix{Int64}:
  6
  9
 12

```

---

<div class="post-metadata">

**Author:** ![Dan\_Micsa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan_micsa/32/30233_2.png) [@Dan\_Micsa](https://discourse.julialang.org/u/Dan_Micsa)\
**Post date:** [September 3, 2023, 10:36am UTC](https://discourse.julialang.org/t/sorting-a-table-dataframe/103475/8 "2023-09-03T10:36:35Z")

</div>

I need to sort after the sum and to have the labels.

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [September 3, 2023, 10:45am UTC](https://discourse.julialang.org/t/sorting-a-table-dataframe/103475/9 "2023-09-03T10:45:05Z")

</div>

All is doable, can you confirm that this is the **exact** way how your input data frame looks like and how your ouput should look? (then I can give you information how to transform `input` into `output`)

```julia
julia> input = DataFrame("*" => ["A", "B", "C"], "A" => 1:3, "B" => 2:4, "C" => 3:5)
3×4 DataFrame
 Row │ * A B C
     │ String Int64 Int64 Int64
─────┼─────────────────────────────
   1 │ A 1 2 3
   2 │ B 2 3 4
   3 │ C 3 4 5

julia> output = DataFrame("*" => ["C", "B", "A", "S"], "C" => [5:-1:3; 12], "B" => [4:-1:2; 9], "A" => [3:-1:1; 6], "S" => [12, 9, 6, "*"])
4×5 DataFrame
 Row │ * C B A S
     │ String Int64 Int64 Int64 Any
─────┼──────────────────────────────────
   1 │ C 5 4 3 12
   2 │ B 4 3 2 9
   3 │ A 3 2 1 6
   4 │ S 12 9 6 *

```

---

<div class="post-metadata">

**Author:** ![Dan\_Micsa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan_micsa/32/30233_2.png) [@Dan\_Micsa](https://discourse.julialang.org/u/Dan_Micsa)\
**Post date:** [September 3, 2023, 10:59am UTC](https://discourse.julialang.org/t/sorting-a-table-dataframe/103475/11 "2023-09-03T10:59:50Z")

</div>

Yes the my problem has a table of over 1000x1000 with labels on raws and columns and I want to have in the top left corner the values that have the highest sums of absolute values. The initial table is matrix in Julia.

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [September 3, 2023, 11:30am UTC](https://discourse.julialang.org/t/sorting-a-table-dataframe/103475/12 "2023-09-03T11:30:52Z")

</div>

Here are the steps to do (I designed them so that they should be hopefully easy to grasp - I did not want to squeeze too much into one step):

```julia
julia> input = DataFrame("*" => ["A", "B", "C"], "A" => 1:3, "B" => 2:4, "C" => 3:5)
3×4 DataFrame
 Row │ * A B C
     │ String Int64 Int64 Int64
─────┼─────────────────────────────
   1 │ A 1 2 3
   2 │ B 2 3 4
   3 │ C 3 4 5

julia> output = select(input, Not("*"))
3×3 DataFrame
 Row │ A B C
     │ Int64 Int64 Int64
─────┼─────────────────────
   1 │ 1 2 3
   2 │ 2 3 4
   3 │ 3 4 5

julia> select!(output, sortperm(sum.(eachcol(output)), rev=true))
3×3 DataFrame
 Row │ C B A
     │ Int64 Int64 Int64
─────┼─────────────────────
   1 │ 3 2 1
   2 │ 4 3 2
   3 │ 5 4 3

julia> output.S = sum(eachcol(output))
3-element Vector{Int64}:
  6
  9
 12

julia> insertcols!(output, 1, "*" => input."*")
3×5 DataFrame
 Row │ * C B A S
     │ String Int64 Int64 Int64 Int64
─────┼────────────────────────────────────
   1 │ A 3 2 1 6
   2 │ B 4 3 2 9
   3 │ C 5 4 3 12

julia> sort!(output, "S", rev=true)
3×5 DataFrame
 Row │ * C B A S
     │ String Int64 Int64 Int64 Int64
─────┼────────────────────────────────────
   1 │ C 5 4 3 12
   2 │ B 4 3 2 9
   3 │ A 3 2 1 6

julia> push!(output, ["S"; sum.(eachcol(output[:, Not(1, end)])); "*"], promote=true)
4×5 DataFrame
 Row │ * C B A S
     │ String Int64 Int64 Int64 Any
─────┼──────────────────────────────────
   1 │ C 5 4 3 12
   2 │ B 4 3 2 9
   3 │ A 3 2 1 6
   4 │ S 12 9 6 *

```

Probably the trickiest part is the last step with `sum.(eachcol(output[:, Not(1, end)]))` where you want to sum everything except first and last column, which are special and you need to manually assign values to them.

---

<div class="post-metadata">

**Author:** ![Dan\_Micsa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan_micsa/32/30233_2.png) [@Dan\_Micsa](https://discourse.julialang.org/u/Dan_Micsa)\
**Post date:** [September 5, 2023, 12:59am UTC](https://discourse.julialang.org/t/sorting-a-table-dataframe/103475/13 "2023-09-05T00:59:32Z")

</div>

Many thx Bogumil!

---

<div class="post-metadata">

**Author:** ![Dan\_Micsa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan_micsa/32/30233_2.png) [@Dan\_Micsa](https://discourse.julialang.org/u/Dan_Micsa)\
**Post date:** [September 6, 2023, 4:54pm UTC](https://discourse.julialang.org/t/sorting-a-table-dataframe/103475/14 "2023-09-06T16:54:01Z")

</div>

I did it like this using matrixes and I receive back the indexes of the rows and columns to aggregate future information.

```julia
function toIxes(m)
    mAbs = abs.(m)
    cSum = sum(mAbs; dims=1)
    rSum = sum(mAbs; dims=2)'
    cIx = sortperm(cSum, dims=2, rev=true)
    rIx = sortperm(rSum, dims=2, rev=true)
    (rIx, cIx)
end 

c = rand(5, 6)

toIxes(c)

```
