Python Forum

Full Version: How to map dataframe to list
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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?

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
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 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.