I have the following dataframe:
df = pd.DataFrame([
('A', 'A', 'Cat'),
('A', 'A', 'Dog'),
('A', 'A', 'Cat'),
('A', 'B', 'Dog'),
('B', 'B', 'Rat'),
('B', 'C', 'Cat'),
('B', 'C', 'Cat')
], columns=['id', 'group', 'Animal'])
I want to group it by id
and group
and calculate the occurrence of Cat
in each group. An example output will be:
[2, 0, 0, 2]
2 cat
in group AA
,
0 cat
in group AB
,
0 cat
in group BB
,
2 cat
in group BC
Can anyone help? Thanks!