I'm working with a DataFrame having the following structure:
import pandas as pd
df = pd.DataFrame({'group':[1,1,1,2,2,2,2,3,3,3],
'brand':['A','B','X','C','D','X','X','E','F','X']})
print(df)
group brand
0 1 A
1 1 B
2 1 X
3 2 C
4 2 D
5 2 X
6 2 X
7 3 E
8 3 F
9 3 X
My goal is to view only the groups having exactly one brand X associated to them. Since group number 2 has two observations equal to brand X, it should be filtered out from the resulting DataFrame.
The output should look like this:
group brand
0 1 A
1 1 B
2 1 X
3 3 E
4 3 F
5 3 X
I know I should do a groupby on the group column and then filter those groups having a count of X different than 1. The filtering part is where I struggle. Any help would be appreciated.