let's create julia dataframe
df=convert(dataframe, rand(10, 4))
it this. trying take transpose of dataframe. "transpose" function appears not working julia data frame shown below.
i have used python pandas dataframe package extensively in past. in python, easy "df.t" please let me know way tranpose dataframe.
i had same question , tried strategy suggested in comments question. problem encountered, however, converting matrix
won't work if dataframe has na
values. have change them else, convert matrix
. had lot of problems converting na
when wanted matrix
dataframe
type.
here's way using dataframe
's stack
, unstack
functions.
df = dataframe(a = 1:4, b = 5:8) df[:id] = 1:size(df, 1) 4×3 dataframes.dataframe │ row │ │ b │ id │ ├─────┼───┼───┼────┤ │ 1 │ 1 │ 5 │ 1 │ │ 2 │ 2 │ 6 │ 2 │ │ 3 │ 3 │ 7 │ 3 │ │ 4 │ 4 │ 8 │ 4 │
adding :id
column suggested dataframe
documentation way unstacking.
now stack columns want transpose:
dfl = stack(df, [:a, :b]) 8×3 dataframes.dataframe │ row │ variable │ value │ id │ ├─────┼──────────┼───────┼────┤ │ 1 │ │ 1 │ 1 │ │ 2 │ │ 2 │ 2 │ │ 3 │ │ 3 │ 3 │ │ 4 │ │ 4 │ 4 │ │ 5 │ b │ 5 │ 1 │ │ 6 │ b │ 6 │ 2 │ │ 7 │ b │ 7 │ 3 │ │ 8 │ b │ 8 │ 4 │
then unstack, switching id , variable names (this why adding :id
column necessary).
dfnew = unstack(dfl, :variable, :id, :value) 2×5 dataframes.dataframe │ row │ variable │ 1 │ 2 │ 3 │ 4 │ ├─────┼──────────┼───┼───┼───┼───┤ │ 1 │ │ 1 │ 2 │ 3 │ 4 │ │ 2 │ b │ 5 │ 6 │ 7 │ 8 │