Используйте пользовательскую функцию с try+except
и передайте DataFrame pipe
:
df = pd.DataFrame({'A':['lorem ipsum','lorem ipsum',np.nan, np.nan],
'B':[["lorem", "foo", "bar"], np.nan, ["lorem", "foo", "bar"], np.nan]})
print (df)
A B
0 lorem ipsum [lorem, foo, bar]
1 lorem ipsum NaN
2 NaN [lorem, foo, bar]
3 NaN NaN
def test(df):
out = []
for a, b in zip(df["A"], df["B"]):
try:
out.append(next(y for y in b if y in a))
except Exception:
out.append('')
return out
df["C"] = df.pipe(test)
print (df)
A B C
0 lorem ipsum [lorem, foo, bar] lorem
1 lorem ipsum NaN
2 NaN [lorem, foo, bar]
3 NaN NaN
Другое решение работает плохо:
df = df.fillna("undefined")
df["C"] = [next((y for y in b if y in a), '') for a, b, in zip(df["A"],df["B"])]
print (df)
A B C
0 lorem ipsum [d, foo, bar]
1 lorem ipsum undefined u
2 undefined [lorem, foo, bar]
3 undefined undefined u