create a summary table with count values

Solution for create a summary table with count values
is Given Below:

my df looks like

group   value
A       1

B       1

A       1

B       1

B       0

B       0

A       0

I want to create a df

value   0      1
group
A       a      b
B       c      d

where a,b,c,d are the counts of 0s and 1s in groups A and B respectively.

I tried group df.groupby('group').size() but that gave an overall count and did not split 0’s and 1s. I tried a groupby count method too but have not been able to achieve the target data frame.

Use pd.crosstab:

pd.crosstab(df['group'], df['value'])

Output:

value  0  1
group      
A      1  2
B      2  2

Use pivot table for this:

res = pd.pivot_table(df, index='group', columns="value", aggfunc="size")

>>>print(res)
value  0  1
group
A      1  2
B      2  2