I am trying to delete a grouped set of rows in pandas according to the following condition:
If a group (grouped by col1) has more than 2 values 'c' in col2, then remove the whole group.
What I have looks like this
col1 col2
0 A 10:10
1 A 20:05
2 A c
3 A 00:10
4 B 04:15
2 B c
3 B c
4 B 13:40
And I am trying to get here:
col1 col2
0 A 10:10
1 A 20:05
2 A c
3 A 00:10
Typically I do this for other very similar dataframes (and it works):
df = df.groupby('col1').filter(lambda x: x["col2"].value_counts()[['c']].sum() < 2)
But for this one is not working and I receive this error:
KeyError: "None of [Index(['c'], dtype='object')] are in the [index]"
Does someone have an idea on how I could do this?
Thanks!