-1

Help to compare two columns in different tables by cycle and return the matches to the first table.

data1:
|name   | revenue |
|-------|---------|
|Alice  | 700     |
|Bob    | 1000    |
|Gerry  | 300     |
|Alex   | 600     |
|Kyle   | 800     |
data2:
|Name   | revenue |
|-------|---------|
|Bob    | 900     |
|Gerry  | 400     |
result data1:
|name   | revenue  |  name_result |
|-------|----------|--------------|
|Alice  | 700      |              |
|Bob    | 1000     |  Bob         |
|Gerry  | 300      |  Gerry       |
|Alex   | 600      |              |
|Kyle   | 800      |              |

I tried using this code, but got all empty values:

import pandas as pd
import numpy as np

def group_category(category):
    for name in data['name']: 
        if name in data2['Name']:
            return name
        else: name = ''
        return name 
data['name_result'] = data['name'].apply(group_category)
Alex
  • 1
  • 2

2 Answers2

0

use:

def group_category(category):
    if category in df2['Name'].unique():
            return category
    else:
        return ''

#Finally:
#Since you are going to use this function on Series so used map() in place of apply()
df1['name_result']=df1['name'].map(group_category)

OR

via isin() and where():

df1['name_result']=df1['name'].where(df1['name'].isin(df2['Name']),'')
Anurag Dabas
  • 23,866
  • 9
  • 21
  • 41
  • How can I overwrite a cell if the conditions are the same and leave the old value if everything else? – Alex Jul 12 '21 at 12:31
0

I found solution:

df1.loc[df1['name'].isin(df2['name_result'].unique()), 'brand'] = 'Adidas Collection'
Alex
  • 1
  • 2