如何根据列值添加另一个df的列值来呈现df?

How to add column values of another df to present df based on the column value?

我有两个数据框。

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

df1 是源数据。现在,如果 df1 的 age 列的值与 df 中的 age 列相匹配,那么它必须将名称附加到 df 的 age 列旁边,并且假设 df 的 age 列中的值为 null 那么它不应该添加任何事物。它也应该为空。它基本上应该根据匹配添加名称。据我所知,我们可以使用合并,但是当我试图合并它时,它创建的额外行不例外,输出也不正确。

给定输出和代码:

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

使用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

您需要一个左连接:

df.merge(df1, on='Age', how='left')