[SOLVED] Check if elements from a Dataframe column are in another Pandas dataframe row and append to a new column

Issue

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
Solo Deportes Remera Michael Jordan Chicago Bulls

and Df2:

Palabra Marca
Acqualine Acqualine
Addnice Addnice
Adnnice Addnice
Under Armour Under Armour
Jordan Nike
Adidas Adidas
Reebok Reebok

How can I check each row of df['Name'], see if the row contains a value of Df2['Palabra'], and in that case get the value of Df2['Marca'] 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
Solo Deportes Remera Michael Jordan Chicago Bulls Nike

Data:

df:

{'Casa': ['Solo Deportes', 'Solo Deportes', 'Solo Deportes', 'Solo Deportes'],
 'Name': ['Paleta De Padel Adidas Metalbone CTRL',
          'Zapatillas Running Under Armour Charged Stamin...',
          'Rompeviento Con Capucha Reebok Woven Azu',
          'Remera Michael Jordan Chicago Bulls']}

Df2:

{'Palabra': ['Acqualine', 'Addnice', 'Adnnice', 'Under Armour', 'Jordan', 'Adidas', 'Reebok'], 
 'Marca': ['Acqualine', 'Addnice', 'Addnice', 'Under Armour', 'Nike', 'Adidas', 'Reebok']}

Solution

A simple solution is to use iterrows in a generator expression and next to iterate over Df2 and check if a matching item appears:

df1['Marca'] = df1['Name'].apply(lambda name: next((r['Marca'] for _, r in Df2.iterrows() if r['Palabra'] in name), float('nan')))

Output:

            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        Reebok
3  Solo Deportes                Remera Michael Jordan Chicago Bulls          Nike

Answered By – enke

Answer Checked By – Mary Flores (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *