0

I have two data frames.

import pandas 
import numpy as np

data = {'Name': ['Tom', 'Joseph', 'Krish', 'John','rack','rox','selena','jha'], 'Age': [20, 21,np.nan , 18,20,30,np.nan,np.nan]}  
df = pd.DataFrame(data)  

print(df)

# Output :
#   Name    Age
# 0 Tom     20.0
# 1 Joseph  21.0
# 2 Krish   NaN
# 3 John    18.0
# 4 rack    20.0
# 5 rox     30.0
# 6 selena  NaN
# 7 jha     NaN

data = {'Named': ['Raj', 'kir', 'cena','ang'], 'Age': [20, 21,18,30]}  
df1 = pd.DataFrame(data)  
    
print(df1)

# Output :    
#   Named Age
# 0 Raj   20
# 1 kir   21
# 2 cena  18
# 3 ang   30

The df1 is a source data. Now, if the value of age column of df1 matches the column age in df then it has to append the name next to the age column of the df and for suppose if the value in age column of df is null then it shouldn't add anything. It should also be null. It should basically add the name based on the match. As far as I know we can use merge but when I am trying to merge its creating extra rows which is not excepted and the output is also not correct.

Given output and code:

dfinal = df.merge(df, left_on="Age",right_on='Age', how = 'outer')

# My output :       
#        Name_x Age Name_y
#    0  Tom     20.0 Tom
#    1  Tom     20.0 rack
#    2  rack    20.0 Tom
#    3  rack    20.0 rack
#    4  Joseph  21.0 Joseph
#    5  Krish   NaN  Krish
#    6  Krish   NaN  selena
#    7  Krish   NaN  jha
#    8  selena  NaN  Krish
#    9  selena  NaN  selena
#    10 selena  NaN  jha
#    11 jha     NaN  Krish
#    12 jha     NaN  selena
#    13 jha     NaN  jha
#    14 John    18.0 John
#    15 rox     30.0 rox
    
    
# My Excepted Output:
#       Name     Age Named 
#    0  Tom     20.0  Raj
#    1  Joseph  21.0  kir
#    2  Krish   NaN   NaN
#    3  John    18.0  cena
#    4  rack    20.0  Raj
#    5  rox     30.0  ang
#    6  selena  NaN   NaN
#    7  jha     NaN   NaN
gustavolq
  • 408
  • 3
  • 10
data en
  • 431
  • 1
  • 2
  • 9

2 Answers2

0

Use how=left

dfinal = df.merge(df1, left_on="Age",right_on='Age', how = 'left')
print(dfinal)

     Name   Age Named
0     Tom  20.0   Raj
1  Joseph  21.0   kir
2   Krish   NaN   NaN
3    John  18.0  cena
4    rack  20.0   Raj
5     rox  30.0   ang
6  selena   NaN   NaN
7     jha   NaN   NaN
Ynjxsjmh
  • 28,441
  • 6
  • 34
  • 52
0

You need a left join:

df.merge(df1, on='Age', how='left')
SomeDude
  • 13,876
  • 5
  • 21
  • 44