Python Forum
How to map dataframe to list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to map dataframe to list
#1
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
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  function returns dataframe as list harum 2 1,337 Aug-13-2022, 08:27 PM
Last Post: rob101
  Using .append() with list vs dataframe Mark17 7 9,902 Jun-12-2022, 06:54 PM
Last Post: Mark17
  List of dataframe values beginning with x,y or z glidecode 3 1,891 Nov-08-2021, 10:16 PM
Last Post: glidecode
  Reading data to python: turn into list or dataframe hhchenfx 2 5,280 Jun-01-2021, 10:28 AM
Last Post: Larz60+
  convert list to five columns dataframe in sequence tonycat 2 2,429 Sep-29-2020, 06:47 AM
Last Post: tonycat
  Get Value from List to Show in DataFrame Column ahmedwaqas92 1 1,873 Jun-22-2020, 08:24 AM
Last Post: ahmedwaqas92
  Inserting a python list into a dataframe column wise mahmoud899 0 4,240 Mar-04-2019, 11:44 PM
Last Post: mahmoud899
  Break Up Tuple List Into A Dataframe? digitalmatic7 2 4,320 Apr-26-2018, 05:29 PM
Last Post: digitalmatic7

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020