You definitely should avoid using pure Python loops when working with Pandas/NumPy etc.
Not sure if this is an optimal solution, especially in case of big
Not sure if this is an optimal solution, especially in case of big
data2
arrays; However, it is working one:df['id'] = sum(df.Name.str.findall("|".join(data2)).map(lambda x: x if x else [pd.np.nan]).tolist(), [])Note: That in future versions of Pandas
pd.np
will be deprecated; you probably need to import numpy directly.