Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to retrieve all the elements from a string that are present in a list

I have the following list and a DataFrame:

the_list = ["one", "et", "allu", "Metall", "54ro", 'al89']

df = pd.DataFrame({ 'ID':[100, 200, 300, 400],
                   'String':['Jonel-al89 (et)', 'Stel-00(et) al89 x 57-mm', 'Metall,   54ro', "allu, Metall9(lop)"]
                  })

What I need is to make a new column where I would get all the elements from the list that are present in each string in the "String" column.
So the output should be looking like that:

ID String Desired_Column
100 Jonel-al89 (et) one, al89, et
200 Stel-00(et) al89 x 57-mm et, al89
300 Metall, 54ro et, Metall, 54ro
400 allu, Metall9(lop) allu, et, Metall

What would be the way to achieve it?
Any help would be much appreciated!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You don’t even need regex if you use a list comprehension which checks for the presence of the elements from your list in the String column.

I’m not sure you want the elements as a list or as string, if you want a string put a str.join around the comprehension.

import pandas as pd

the_list = ["one", "et", "allu", "Metall", "54ro", 'al89']

df = pd.DataFrame({ 'ID':[100, 200, 300, 400],
                   'String':['Jonel-al89 (et)', 'Stel-00(et) al89 x 57-mm', 'Metall,   54ro', "allu, Metall9(lop)"]
                  })

df["Desired_Column"] = df["String"].apply(lambda string: [el for el in the_list if el in string])

df
# gives
#     ID                    String      Desired_Column
# 0  100           Jonel-al89 (et)     [one, et, al89]
# 1  200  Stel-00(et) al89 x 57-mm          [et, al89]
# 2  300            Metall,   54ro  [et, Metall, 54ro]
# 3  400        allu, Metall9(lop)  [et, allu, Metall]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading