I want to count the occurrence of a string in a grouped pandas dataframe column.
Assume I have the following Dataframe:
catA catB scores
A X 6-4 RET
A X 6-4 6-4
A Y 6-3 RET
B Z 6-0 RET
B Z 6-1 RET
First, I want to group by catA
and catB
. And for each of these groups I want to count the occurrence of RET
in the scores
column.
The result should look something like this:
catA catB RET
A X 1
A Y 1
B Z 2
The grouping by two columns is easy: grouped = df.groupby(['catA', 'catB'])
But what's next?