Reputation: 5279
I have df
like following
A B C
a a d
a b d
a b e
b c e
b c f
When I try
df.groupby(A).size()
A
a 3
b 2
df.groupby(B).size()
B
a 1
b 2
c 2
my desired result is aggregated one
A B
a 3 1
b 2 2
c 0 2
Are there any way to achieve this result ?
If someone has opinion,please let me know.
Thanks
Upvotes: 2
Views: 41
Reputation: 323226
melt
+ crosstab
s = df[['A','B']].melt()
out = pd.crosstab(s['value'],s['variable'])
out
Out[18]:
variable A B
value
a 3 1
b 2 2
c 0 2
Or
df[['A','B']].apply(pd.Series.value_counts)
Out[19]:
A B
a 3.0 1
b 2.0 2
c NaN 2
Upvotes: 1