Python Pandas:带有aggfunc的数据透视表=计数唯一唯一 [英] Python Pandas : pivot table with aggfunc = count unique distinct
问题描述
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')
回溯(最近一次通话最近):... AttributeError:索引"对象 没有属性索引"
Traceback (most recent call last): ... AttributeError: 'Index' object has no attribute 'index'
如何获得一个数据帧表,其中一个数据帧列具有唯一值计数,而另外两个列又具有此值?
是否有aggfunc
个唯一计数?我应该使用np.bincount()
吗?
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.我知道'Series'values_counts()
,但是我需要一个数据透视表.
NB. I am aware of 'Series' values_counts()
however I need a pivot table.
输出应为:
Z Z1 Z2 Z3
Y
Y1 1 1 NaN
Y2 NaN NaN 1
推荐答案
您的意思是这样的吗?
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
请注意,使用len
假定您的DataFrame中没有NA
.否则,您可以执行x.value_counts().count()
或len(x.dropna().unique())
.
Note that using len
assumes you don't have NA
s in your DataFrame. You can do x.value_counts().count()
or len(x.dropna().unique())
otherwise.
这篇关于Python Pandas:带有aggfunc的数据透视表=计数唯一唯一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!