Hello,
So I have a table like
df = DataFrame(a= [1,1,2,2], b = [1,2,1,2], c = [1,2,3,4])
And I would like to create a table that, if I were to work in Excel, I could obtain with a pivot table with a for the Rows and b for the Columns. I assume uniqueness in the combinations {a,b} but in Excel youβd have to ask for the sum of c for the Values field.
The result would be as follows:
a  | b=1  b=2
------------------
1  | 1    2
2  | 3    4
Iβm trying to reproduce this with DataFrames(Meta) but I canβt figure out how to do that. Is there a built in way to do it or will I have to program it myself ?
For the rows itβs easy, using @by or groupby does it, but working with columns seems less supported.
             
            
              
              
              
            
            
           
          
            
            
              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
            
            
           
          
            
            
              Great ressources. Thanks a lot !