I'm trying to sort a pandas data frame by rows that have two specific values in any column. In the sample data below, I would want to select the rows that have a value of 'apple' AND 'grape',
a b c
0 apple orange grape
1 grape apple banana
2 pear kiwi apple
resulting in a filtered data frame that shows:
a b c
0 apple orange grape
1 grape apple banana
Using the the code below, I can select all the rows that have one specific value:
df[(df == 'orange').any(axis=1)]
The result retuned, as expected, was:
a b c
0 apple orange grape
Using the following line of code, I expected to select the rows that had both values somewhere in the row, but this returned all the rows that had either apple OR grape as a column value:
df[np.isin(df, ['apple', 'grape']).any(axis=1)]
I expected to get only the rows that had apple AND grape using the previous line, but that obviously isn't the correct way to accomplish this. How do I go about selecting rows that only have both values in any column?