Python Pandas : pivot table with aggfunc = count unique distinct

后端 未结 8 1743
谎友^
谎友^ 2020-12-07 13:02
df2 = pd.DataFrame({\'X\' : [\'X1\', \'X1\', \'X1\', \'X1\'], \'Y\' : [\'Y2\',\'Y1\',\'Y1\',\'Y1\'], \'Z\' : [\'Z3\',\'Z1\',\'Z1\',\'Z2\']})

    X   Y   Z
0  X1  Y2         


        
相关标签:
8条回答
  • 2020-12-07 13:50

    You can construct a pivot table for each distinct value of X. In this case,

    for xval, xgroup in g:
        ptable = pd.pivot_table(xgroup, rows='Y', cols='Z', 
            margins=False, aggfunc=numpy.size)
    

    will construct a pivot table for each value of X. You may want to index ptable using the xvalue. With this code, I get (for X1)

         X        
    Z   Z1  Z2  Z3
    Y             
    Y1   2   1 NaN
    Y2 NaN NaN   1
    
    0 讨论(0)
  • 2020-12-07 13:53

    Since at least version 0.16 of pandas, it does not take the parameter "rows"

    As of 0.23, the solution would be:

    df2.pivot_table(values='X', index='Y', columns='Z', aggfunc=pd.Series.nunique)
    

    which returns:

    Z    Z1   Z2   Z3
    Y                
    Y1  1.0  1.0  NaN
    Y2  NaN  NaN  1.0
    
    0 讨论(0)
提交回复
热议问题