Python Pandas : pivot table with aggfunc = count unique distinct

后端 未结 8 1751
谎友^
谎友^ 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:40

    Do you mean something like this?

    In [39]: df2.pivot_table(values='X', rows='Y', cols='Z', 
                             aggfunc=lambda x: len(x.unique()))
    Out[39]: 
    Z   Z1  Z2  Z3
    Y             
    Y1   1   1 NaN
    Y2 NaN NaN   1
    

    Note that using len assumes you don't have NAs in your DataFrame. You can do x.value_counts().count() or len(x.dropna().unique()) otherwise.

提交回复
热议问题