Python 基于两个条件从数据帧中提取值

Python 基于两个条件从数据帧中提取值,python,pandas,dataframe,Python,Pandas,Dataframe,我有两个数据帧,df1和df2 df1 = A B 1 a 1 1 5 1 b 1 c 1 d df2 = A B C 1 a apple 1 cherry 1 5 apple 1 b orange 我想基于A列和B列合并这两个数据帧。我的逻辑如下: if df1['A'][0] is in df2['A'] and df1['B'][0] is in df2['B'

我有两个数据帧,
df1
df2

df1 = A B
      1 a
      1 
      1 5
      1 b
      1 c
      1 d


df2 = A B C
      1 a apple
      1  cherry
      1 5 apple
      1 b orange
我想基于A列和B列合并这两个数据帧。我的逻辑如下:

if df1['A'][0] is in df2['A'] and df1['B'][0] is in df2['B'] and they are equal:

      then create new column df1['New Product'] = df2['C'] 
如果满足此条件,我需要在
df1
中创建第三列

我努力了,但没有成功。我想指数位置很重要

这是我的解决方案,但不起作用:

df1['New Product'] = df2['C'][(df1['A'].isin(df2['A'])) & (df1['B'].isin(df2['B']))] 
预期产出应为:

df1 = A B C
      1 a apple
      1  cherry
      1 5 apple
      1 b orange
      1 c nan
      1 d nan 
尝试简单的左连接

df=pd.merge(df1,df2,on=['A','B'],how='left').rename(columns={'C':'New Product'})
O/p:

你需要:

import pandas as pd

df1 = pd.DataFrame({'A':[1]*6, 'B':['a',None,5,'b','c','d']})
df2 = pd.DataFrame({'A':[1]*4, 'B':['a', None, 5, 'b'], 'C':['apple','cherry','apple','orange']})

df = df1.merge(df2, how='left', on=['A','B'])
print(df)
输出:

 A     B       C                                                                                                                    
0  1     a   apple                                                                                                                    
1  1  None  cherry                                                                                                                    
2  1     5   apple                                                                                                                    
3  1     b  orange                                                                                                                    
4  1     c     NaN                                                                                                                    
5  1     d     NaN    

你能分享预期的输出吗?你想要一个左
merge
,对于这个,
df.merge(df2,on=['a','B'],how='left')
似乎可以工作,但是在这之后新
df
的长度增加了,但我这里不需要这个,当我执行此合并时,新的
df
的长度大于原始
df1
@NodarOkroshiashvili的长度-这意味着您的df2在键列中有重复值,首先使用df2=df2删除该值。删除重复项(子集=['A','B']),然后执行上述代码谢谢,伙计,这非常有效。我这么做了,但似乎错过了什么
 A     B       C                                                                                                                    
0  1     a   apple                                                                                                                    
1  1  None  cherry                                                                                                                    
2  1     5   apple                                                                                                                    
3  1     b  orange                                                                                                                    
4  1     c     NaN                                                                                                                    
5  1     d     NaN