Given the following dataframe
df = pd.DataFrame({'A':list('AAAAAABBBBBCCCCCC'),
'B':list('EFGHIJEGHJKGHJKEI')})
| | A | B |
|---:|:----|:----|
| 0 | A | E |
| 1 | A | F |
| 2 | A | G |
| 3 | A | H |
| 4 | A | I |
| 5 | A | J |
| 6 | B | E |
| 7 | B | G |
| 8 | B | H |
| 9 | B | J |
| 10 | B | K |
| 11 | C | G |
| 12 | C | H |
| 13 | C | J |
| 14 | C | K |
| 15 | C | E |
| 16 | C | I |
I would like to find all the elements in A where B contains "G", and "H", and "I"
therefore, the result should be
| | A | B |
|---:|:----|:----|
| 2 | A | G |
| 3 | A | H |
| 4 | A | I |
| 11 | C | G |
| 12 | C | H |
| 16 | C | I |
For the moment, I have found the following solution, but that seems overly hackish and I feel I'm missing something obvious
hit = list('GHI')
out = df[df.groupby('A').apply(lambda x: (x['B'].isin(hit))&(x['B'].isin(hit).sum()==len(hit))).values]