Home > Enterprise >  Check if elements from a Dataframe column are in another Pandas dataframe row and append to a new co
Check if elements from a Dataframe column are in another Pandas dataframe row and append to a new co

Time:03-16

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']}

CodePudding user response:

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
  • Related