Aug-07-2020, 05:48 PM
Hi,
I have a dataframe and and I want to map to list. If list elemnt exist in 'Name' column, then record this list item in column 'id'.
I use the below code but it is slower when the datsize is too big. Is there and other method which can make this loop faster?
I have a dataframe and and I want to map to list. If list elemnt exist in 'Name' column, then record this list item in column 'id'.
I use the below code but it is slower when the datsize is too big. Is there and other method which can make this loop faster?
import pandas as pd data = {'Name':['AB TV987UI xt', 'He L987UI 0?', 'List M9JL7 exist', 'The M78IU09 of lists'], 'Age':[20, 21, 19, 18]} df = pd.DataFrame(data) df['id'] = 'NA' data2 = ['TV987UI', 'L987UI', 'M9JL7', 'M78IU09'] for item in data2: for idx, row in df.iterrows(): if item in row['Name']: df.loc[idx,'id']= item else: continue