我想知道是否有可能通过给出一个’if’语句来合并两个数据框.
例如
df
number account_name classification
1 name named
2 place partner
3 animal class
4 thing territory
5 dog home
df1
account_name Number country
name 1 xx
place 2 xy
animal 7 yz
dog 8 zx
我正在寻找下面的代码
pd.merge(df,df1, on= 'account_name') if df[number] == df1[number]
结果就像
number account_name classification number Country
1 name named 1 xx
2 place partner 2 xy
我也尝试了lambda函数,代码是
x['nn'] = x.apply(lambda y: pd.merge(df, df1, on = 'account_name') if df[number] == df1[number] else 1, axis=1)
解决方法:
看来您需要在以下参数上添加列:
df = pd.merge(df,df1, on= ['account_name', 'number'])
print (df)
number account_name classifi