Excel's pivot table equivalent with DataFramesMeta

Here’s how to do it using unstack in DataFrames.jl

julia> df = DataFrame(a= [1,1,2,2], b = [1,2,1,2], c = [1,2,3,4])
4×3 DataFrame
 Row │ a      b      c     
     │ Int64  Int64  Int64 
─────┼─────────────────────
   1 │     1      1      1
   2 │     1      2      2
   3 │     2      1      3
   4 │     2      2      4

julia> unstack(df, :b, :c; renamecols = v -> "b_$v")
2×3 DataFrame
 Row │ a      b_1     b_2    
     │ Int64  Int64?  Int64? 
─────┼───────────────────────
   1 │     1       1       2
   2 │     2       3       4

For a more complicated prolem, where you have multiple value variables (instead of just :c like you have here), see this thread.

Here is a general explainer for doing “pivot table”-like operations with DataFrames

2 Likes