Q:

Faster method for finding any matching values in subset of pandas columns against a list

I have a very, very large dataframe that resembles this:
+---+--------+--------+--------+--------+
|   | code_1 | code_2 | code_3 | code_4 |
+---+--------+--------+--------+--------+
| 1 | abc    | def    | ghi    | jkl    |
| 2 | zzz    | zzz    | zzz    | zzz    |
| 3 | def    | zzz    | zzz    | zzz    |
| 4 | abc    | abc    | abc    | abc    |
| 5 | lol    | lol    | lol    | lol    |
+---+--------+--------+--------+--------+

I need to go through each row and match the set a codes against a set of approved codes.  For the example above, the set of approved codes is ['abc', 'def']
If a given rows has any matches against the approved codes I would like to return True to add to a new column, otherwise return False.
Given the approved codes above, the output would be as follows:
+---+--------+--------+--------+--------+------------+
|   | code_1 | code_2 | code_3 | code_4 | is_primary |
+---+--------+--------+--------+--------+------------+
| 1 | abc    | def    | ghi    | jkl    | TRUE       |
| 2 | zzz    | zzz    | zzz    | zzz    | FALSE      |
| 3 | def    | zzz    | zzz    | zzz    | TRUE       |
| 4 | abc    | abc    | abc    | abc    | TRUE       |
| 5 | lol    | lol    | lol    | lol    | FALSE      |
+---+--------+--------+--------+--------+------------+

Right now my method for performing this task is as follows:
def assign_primary(row, codes):
    is_primary = any(i in row.values for i in codes)

    if is_primary:
        return True
    return False

df['is_primary'] = df.apply(lambda row: assign_primary(
                            row, ['abc', 'def']), axis=1)

This works, but is impossibly slow on my large data set.  I am matching 15 cols against an approved code list of 100 over 2 million rows.
I'm looking for faster implementations if anyone has methods for doing this.

A:

Try use DataFrame.isin for check by list with DataFrame.any for check at least one True per row:
print (df.isin(['abc', 'def']))
   code_1  code_2  code_3  code_4
1    True    True   False   False
2   False   False   False   False
3    True   False   False   False
4    True    True    True    True
5   False   False   False   False

df['is_primary'] = df.isin(['abc', 'def']).any(axis=1)
print (df)
  code_1 code_2 code_3 code_4  is_primary
1    abc    def    ghi    jkl        True
2    zzz    zzz    zzz    zzz       False
3    def    zzz    zzz    zzz        True
4    abc    abc    abc    abc        True
5    lol    lol    lol    lol       False

Solution with subset:
df['is_primary'] = df[['code_2','code_3']].isin(['abc', 'def']).any(axis=1)
print (df)
  code_1 code_2 code_3 code_4  is_primary
1    abc    def    ghi    jkl        True
2    zzz    zzz    zzz    zzz       False
3    def    zzz    zzz    zzz       False
4    abc    abc    abc    abc        True
5    lol    lol    lol    lol       False

