Метод 1: list comprehension & zip
:
Мы можем использовать list comprehension
с zip
здесь для сравнения строк по элементам:
df['NOTE'] = [any([x in a for x in b]) for a, b in zip(df['A'], df['B'])]
df['NO_MATCH'] = [[x for x in b if x in a] for a, b in zip(df['A'], df['B'])]
A B NOTE NO_MATCH
0 [131, 138, 137, 140, 141, 142, 35] [137, 35] True [137, 35]
1 [11, 18, 59, 33, 61, 12, 13, 15, 60, 16] [137, 33, 16] True [33, 16]
2 [21, 22, 23, 24, 25, 26, 27, 28, 29] [20, 19, 33] False []
Метод2: intersection with set()
:
df['NOTE'] = [any(set(a) & set(b)) for a, b in zip(df['A'], df['B'])]
df['NO_MATCH'] = [list(set(a) & set(b)) for a, b in zip(df['A'], df['B'])]
A B NOTE NO_MATCH
0 [131, 138, 137, 140, 141, 142, 35] [137, 35] True [137, 35]
1 [11, 18, 59, 33, 61, 12, 13, 15, 60, 16] [137, 33, 16] True [16, 33]
2 [21, 22, 23, 24, 25, 26, 27, 28, 29] [20, 19, 33] False []
Пересечение :
Примечание 1 : Если вы хотите yes
, no
вместо True
, False
, используйте Series.map
:
df['NOTE'] = df['NOTE'].map({True: 'yes', False: 'no'})
A B NOTE NO_MATCH
0 [131, 138, 137, 140, 141, 142, 35] [137, 35] yes [137, 35]
1 [11, 18, 59, 33, 61, 12, 13, 15, 60, 16] [137, 33, 16] yes [16, 33]
2 [21, 22, 23, 24, 25, 26, 27, 28, 29] [20, 19, 33] no []
Примечание 2: Плюс, если вы хотите NO_MATCH
как str
вывод:
df['NO_MATCH'] = df['NO_MATCH'].astype(str).str.replace('[\[\]]', '')
A B NOTE NO_MATCH
0 [131, 138, 137, 140, 141, 142, 35] [137, 35] yes 137, 35
1 [11, 18, 59, 33, 61, 12, 13, 15, 60, 16] [137, 33, 16] yes 16, 33
2 [21, 22, 23, 24, 25, 26, 27, 28, 29] [20, 19, 33] no
Примечание3 : Если ваш список является строковым представлением списка, используйте ast.literal_eval
:
print(df)
A B
0 [131, 138, 137, 140, 141, 142,35] [137,35]
1 [11, 18, 59, 33, 61, 12, 13, 15, 60, 16] [137,33,16]
2 [21, 22, 23, 24, 25, 26, 27, 28, 29] [20,19,33]
type(df['A'].iat[0])
# Out: str
from ast import literal_eval
df['A'] = df['A'].apply(literal_eval)
df['B'] = df['B'].apply(literal_eval)
type(df['A'].iat[0])
# Out: list