Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order columns alphabetically in dataframe Julia

I have a dataframe like this:

using DataFrames

df = DataFrame(C = [1,2,3],
               A = [1,1,1], 
               B = [3,2,1])
3×3 DataFrame
 Row │ C      A      B     
     │ Int64  Int64  Int64 
─────┼─────────────────────
   1 │     1      1      3
   2 │     2      1      2
   3 │     3      1      1

I would like to alphabetically order these columns which would be of course: A,B and C. How can we do this in a dataframe Julia? It could be more than 3 columns. In R we could use the order function.

like image 679
Quinten Avatar asked Nov 26 '25 03:11

Quinten


1 Answers

How about using names with sortperm

julia> df[:, sortperm(names(df))]
3×3 DataFrame
 Row │ A      B      C     
     │ Int64  Int64  Int64 
─────┼─────────────────────
   1 │     1      3      1
   2 │     1      2      2
   3 │     1      1      3
like image 88
Andre Wildberg Avatar answered Nov 28 '25 01:11

Andre Wildberg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!