Python Forum
How to map dataframe to list - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to map dataframe to list (/thread-28877.html)



How to map dataframe to list - Mekala - Aug-07-2020

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



RE: How to map dataframe to list - scidam - Aug-09-2020

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.