概述
df2 = pd.DataFrame({'X' : ['X1', 'X1', 'X1', 'X1'], 'Y' : ['Y2','Y1','Y1','Y1'], 'Z' : ['Z3','Z1','Z1','Z2']})
X Y Z
0 X1 Y2 Z3
1 X1 Y1 Z1
2 X1 Y1 Z1
3 X1 Y1 Z2
g=df2.groupby('X')
pd.pivot_table(g, values='X', rows='Y', cols='Z', margins=False, aggfunc='count')
Traceback (most recent call last): ... AttributeError: 'Index' object
has no attribute 'index'
How do I get a Pivot Table with counts of unique values of one DataFrame column for two other columns?
Is there aggfunc for count unique? Should I be using np.bincount()?
NB. I am aware of 'Series' values_counts() however I need a pivot table.
EDIT: The output should be:
Z Z1 Z2 Z3
Y
Y1 1 1 NaN
Y2 NaN NaN 1
解决方案
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.
最后
以上就是和谐台灯为你收集整理的python数据透视表计数去除重复,Python Pandas:带有aggfunc的数据透视表=计数唯一唯一...的全部内容,希望文章能够帮你解决python数据透视表计数去除重复,Python Pandas:带有aggfunc的数据透视表=计数唯一唯一...所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复