简体   繁体   中英

Check if Python list elements are in a Pandas dataframe row and append to a new column

I have a dataframe like this:

Casa Name
Solo Deportes Paleta De Padel Adidas Metalbone CTRL
Solo Deportes Zapatillas Running Under Armour Charged Stamin...
Solo Deportes Rompeviento Con Capucha Reebok Woven Azu

and a List:

Maestro_Marcas=['Adidas', 'Reebebok','Under Armour']

How can I check each row of df['Name'], see if the row contains a value of the list, and in that case get the value of the list and put it in the new column?. The result should be something like this:

Casa Name Marca
Solo Deportes Paleta De Padel Adidas Metalbone CTRL Adidas
Solo Deportes Zapatillas Running Under Armour Charged Stamin... Under Armour
Solo Deportes Rompeviento Con Capucha Reebok Woven Azu Reebok

you can use apply with a function such that:

Maestro_Marcas=['Adidas', 'Reebebok','Under Armour']

def is_exist(name):
    for i in Maestro_Marcas:
        if i in name:
            return i

df['Marca']  = df.apply(lambda x: is_exist(x['Name']), axis=1)

desired result:

    Casa            Name                                                 Marca   
0   Solo Deportes   Paleta De Padel Adidas Metalbone CTRL               Adidas
1   Solo Deportes   Zapatillas Running Under Armour Charged Stamin...   Under Armour
2   Solo Deportes   Rompeviento Con Capucha Reebok Woven Azu        None

I'm quite sure you should improve the function at one point for Reebebok vs Reebok adjust as you see fit.

You can try findall

df['Marca'] = df['Name'].str.findall('|'.join(Maestro_Marcas))

Notice,it will return a list of finding, if two items found within one Name, it will return both.

To match the output you need

 df['Marca'] = df['Name'].str.findall('|'.join(Maestro_Marcas)).str[0]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM