Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is pandas' transpose equivalent in Julia

Tags:

pandas

julia

What is pandas' transpose equivalent in Julia? thanks

I like to transpose a data frame and transpose function isn't working.

like image 564
siebenstein Avatar asked Dec 04 '25 06:12

siebenstein


2 Answers

It is permutedims, it turns a data frame on its side such that rows become columns and values in the column become the names.

like image 84
barpapapa Avatar answered Dec 07 '25 00:12

barpapapa


Note the difference between transpose and permutedims in Julia Base. permutedims only affects the outermost container. transpose is recursive.

Here are the consequences:

julia> x = [randstring(3) for _ in 1:3, _ in 1:3]
3×3 Matrix{String}:
 "nTa"  "QBM"  "3dJ"
 "RsL"  "mD1"  "3jq"
 "dFp"  "bfB"  "k6P"

julia> permutedims(x)
3×3 Matrix{String}:
 "nTa"  "RsL"  "dFp"
 "QBM"  "mD1"  "bfB"
 "3dJ"  "3jq"  "k6P"

julia> transpose(x)
3×3 transpose(::Matrix{String}) with eltype Union{}:
Error showing value of type LinearAlgebra.Transpose{Union{}, Matrix{String}}:
ERROR: MethodError: no method matching transpose(::String)

julia> y = [rand(3) for _ in 1:3, _ in 1:3]
3×3 Matrix{Vector{Float64}}:
 [0.446435, 0.653228, 0.0857836]  [0.378189, 0.505487, 0.0504642]  [0.0570918, 0.462984, 0.800813]
 [0.801857, 0.75505, 0.714087]    [0.253316, 0.458364, 0.80242]    [0.93742, 0.699745, 0.140957]
 [0.419783, 0.22946, 0.748267]    [0.445365, 0.563222, 0.363561]   [0.088825, 0.0869342, 0.311187]

julia> permutedims(y)
3×3 Matrix{Vector{Float64}}:
 [0.446435, 0.653228, 0.0857836]  [0.801857, 0.75505, 0.714087]  [0.419783, 0.22946, 0.748267]
 [0.378189, 0.505487, 0.0504642]  [0.253316, 0.458364, 0.80242]  [0.445365, 0.563222, 0.363561]
 [0.0570918, 0.462984, 0.800813]  [0.93742, 0.699745, 0.140957]  [0.088825, 0.0869342, 0.311187]

julia> transpose(y) # note that inside we have 1x3 objects not vectors
3×3 transpose(::Matrix{Vector{Float64}}) with eltype LinearAlgebra.Transpose{Float64, Vector{Float64}}:
 [0.446435 0.653228 0.0857836]  [0.801857 0.75505 0.714087]  [0.419783 0.22946 0.748267]
 [0.378189 0.505487 0.0504642]  [0.253316 0.458364 0.80242]  [0.445365 0.563222 0.363561]
 [0.0570918 0.462984 0.800813]  [0.93742 0.699745 0.140957]  [0.088825 0.0869342 0.311187]

In DataFrames.jl we decided that this recursive behavior (which makes sense in linear algebra context) is not desirable. You can even read this in the docstring of transpose which states:

This operation is intended for linear algebra usage - for general data manipulation see permutedims, which is non-recursive.

Additionally in DataFrames.jl permutedims requires you to specify the column which will become column names after the operation (this requirement is DataFrames.jl specific) and you need to be careful as eltype promotion is performed (this issue is not visible for matrices which have a common eltype for all elements, while in a data frame each column might have a diffeent eltype):

julia> df1 = DataFrame(rowkey=["x", "y"], b=[1.0, 2.0], c=[3, 4], d=[true, false])
2×4 DataFrame
 Row │ rowkey  b        c      d
     │ String  Float64  Int64  Bool
─────┼───────────────────────────────
   1 │ x           1.0      3   true
   2 │ y           2.0      4  false

julia> df2 = permutedims(df1, :rowkey)
3×3 DataFrame
 Row │ rowkey  x        y
     │ String  Float64  Float64
─────┼──────────────────────────
   1 │ b           1.0      2.0
   2 │ c           3.0      4.0
   3 │ d           1.0      0.0

julia> permutedims(df2, :rowkey)
2×4 DataFrame
 Row │ rowkey  b        c        d
     │ String  Float64  Float64  Float64
─────┼───────────────────────────────────
   1 │ x           1.0      3.0      1.0
   2 │ y           2.0      4.0      0.0
like image 38
Bogumił Kamiński Avatar answered Dec 06 '25 23:12

Bogumił Kamiński



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!