Let's say I have a dataframe like this:
v1 v2
a 1
a 1
b 1
b 2
I would like to groupby on v1 and make the count of each possible value in v2. So the result would be something like:
v2
1 2
v1
a 2 0
b 1 1
I can do something like this
df.groupby("v1")\
.agg(
{
"v2": {
"0": lambda x: sum(x==0),
"1": lambda x: sum(x==1)
}
}
}
But it's not really nice if the number of values is hight, or change! I've seen this post but couldn't make it working with my example.
Thanks for your help :)