New DataFrame whose columns are values of a column and grouped by another column

How can I convert df1 to df2:

julia> df1 = DataFrame(:name => ["a", "b", "c", "c", "a", "b"], :count => [1,4,3,6,3,7], :time => [1,1,1,2,2,2])
6×3 DataFrame
 Row │ name      count     time  
     │ String  Int64  Int64 
─────┼──────────────────────
   1 │ a           1      1
   2 │ b           4      1
   3 │ c           3      1
   4 │ c           6      2
   5 │ a           3      2
   6 │ b           7      2

julia> df2= DataFrame(:a => [1, 3], :b=> [4, 7], :c => [3, 6], time => [1,2])
2×4 DataFrame
 Row │ a      b      c      time  
     │ Int64  Int64  Int64  Int64 
─────┼────────────────────────────
   1 │     1      4      3      1
   2 │     3      7      6      2


This is called converting from long format to wide format. You can use unstack for this (and stack to go the other way):

julia> unstack(df1, :name, :count)
2×4 DataFrame
 Row │ time   a       b       c      
     │ Int64  Int64?  Int64?  Int64? 
─────┼───────────────────────────────
   1 │     1       1       4       3
   2 │     2       3       7       6

(and unstack(df1, :name, :count) |> disallowmissing will get rid of the Union{Missing, ...} column types.)

2 Likes

Nice! Thank you!