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 |
CodePudding user response:
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.
CodePudding user response:
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]